SELECT test_id
, testdescription
FROM tbltest AS t
WHERE NOT EXISTS
(SELECT *
FROM tbltestprofile tp
WHERE t.test_id = tp.test_id
AND tp.profile_id = X
)
旁注。如果您将表格(和字段)的名称保持简单并且不添加tbl
前缀(如test
,profile
,testprofile
),将会有所帮助。一个简单的三表加入你可能已经使用:
SELECT tbltest.test_id
, tbltest.testdescription
, tblprofile.profile_id
, tblprofile.profiledescription
FROM tbltest
JOIN tbltestprofile
ON tbltest.test_id = tbltestprofile.test_id
JOIN tblprofile
ON tblprofile.profile_id = tbltestprofile.profile_id
ORDER BY tblprofile.profiledescription
让我相当头晕。这不是更好吗?即使没有别名:
SELECT test.id AS test_id
, test.description AS test
, profile.id AS profile_id
, profile.description AS profile
FROM test
JOIN testprofile
ON test.id = testprofile.test_id
JOIN profile
ON profile.id = testprofile.profile_id
ORDER BY profile.description
您的要求混淆了我。 “testprofile中没有匹配记录”和“profile_id = x”这两个条件如何都为真?这些对我来说似乎是矛盾的。我认为你需要提供更多信息。 – Flimzy