2014-02-27 180 views
0

這是我的表格結構。通過連接從超過2個mysql表中獲取數據。

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:我們需要找出那些沒有被特定用戶閱讀的問題。

回答

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

待辦事項你的意思是'reader_id',而不是'user_id'? –

+0

哦,是的,我的壞,對不起。 - 編輯。 –

+0

感謝您的快速回復Loic我們還需要來自Table2的電子郵件,因爲Table2保存了發佈問題和閱讀的人員的信息。 –

0

FROM表1 SELECT問題其中id NOT IN(SELECT question_id FROM表3,其中reader_id IN(SELECT ID FROM表2其中username = 'XXXX'))

0

試試這個:

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; 
+0

它顯示語法錯誤JASON –

+0

最新的錯誤SP?你可以在這裏發佈嗎? – avisheks