简体   繁体   English

使用join从2个以上的mysql表中获取数据。

[英]Getting data from more than 2 mysql tables with join.

Here is my tables structure. 这是我的表格结构。

Table1  // it has 4 colums,that saves all the question posted by user

id|question|answer|postby|

Table2  //it has 2 colums, that saves all the signed up users

id|username|email|

Table3 //it saves that which question is read by which user.

id|reader_id|question_id| 

NOTE:reader_id is the id of table 2. 注意:reader_id是表2的ID。

Problem: We need to find out those questions that are not being read by a particular user. 问题:我们需要找出特定用户没有阅读的那些问题。

select * from table1 where id not in (
    select question_id from table3 where reader_id = [particular user id]
)

从表1中选择ID不输入的问题(在表1中选择问题ID的读者(在用户名='XXXX'的情况下从表2选择的ID))

Try this: 尝试这个:

SELECT t1.*
FROM table1 t1
LEFT JOIN
  (SELECT t3.* 
   FROM 
   table2 t2
   JOIN table3 t3 ON t3.reader_id=t2.id
   WHERE t3.reader_id=SOME USER)t ON t.question_id=t1.id
WHERE t3.id IS NULL;

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

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