簡體   English   中英

使用join從2個以上的mysql表中獲取數據。

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

這是我的表格結構。

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| 

注意:reader_id是表2的ID。

問題:我們需要找出特定用戶沒有閱讀的那些問題。

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))

嘗試這個:

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