2011-09-14 51 views
2

好的..我有2個表格:test_scorestest_exemptionsMysql加入幫助

我需要選擇test_scoreid以僅顯示未被豁免的測試項目。

相關字段:

test_scores

test_scoreid test_score_sid test_score_testitemid 

豁免

testexemptionid exemption_sid exemption_testitemid 

任何幫助是極大的讚賞,因爲這已被我發瘋。

+0

哪些字段(S)可以有兩個表中常見的數據? –

+0

歡迎的StackOverflow!如果你是新來的人,請記住(認爲是最好的)(接受答案)(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)解決你的問題。 – Marco

+0

令人敬畏的馬爾科..謝謝! – Henrik

回答

0

我認爲兩個表是加入與領域test_score_testitemidexemption_testitemid
試試這個:

SELECT ts.test_scoreid, te.testexemptionid 
FROM test_scores ts LEFT JOIN exemptions te 
ON ts.test_score_testitemid = te.exemption_testitemid 
WHERE te.testexemptionid IS NULL 

SELECT ts.test_scoreid 
FROM test_scores ts 
WHERE ts.test_score_testitemid NOT IN 
(SELECT DISTINCT exemption_testitemid FROM exemptions) 
+0

@亨利克:你解決了你的問題嗎? – Marco