简体   繁体   English

使用join从2个以上的表中获取记录

[英]Getting records from more than 2 tables using join

Here is my table structure: 这是我的表结构:

Table 1 表格1

  id | question | posted_id

Table 2 表2

  id | username | email  

Table 3 表3

 id | reader_id | quote_id

I want to fetch those question which are not read by particular user.I also want email from table2 in result set. 我想获取特定用户未读的那些问题。我还希望结果集中来自table2的电子邮件。 So I created Below query that join my two table1 and table2. 所以我创建了以下查询,将我的两个table1和table2连接在一起。 My query: 我的查询:

SELECT table1.id,table1.question,table1.posted_id,table2.email 
FROM table1,table2 
WHERE table1.post_id = table.id;   

How to fix a left join on table 3 for filter the records Because I need questions which are not read by particular user. 如何在表3上固定左联接以过滤记录,因为我需要特定用户无法阅读的问题。

I am not sure which user field you are trying to match against, i have taken email, Also this will only give matching records, Try 我不确定要与之匹配的用户字段,我已经收到了电子邮件,这也只会提供匹配的记录,请尝试

SELECT T1.*,T2.email
FROM Table1 AS T1
INNER JOIN Table2 AS T2 ON T2.quote_id = T1.id
INNER JOIN Table3 AS T3 ON T3.reader_id = T2.id
WHERE T2.email='some_mail_id'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM