我試圖找到只存在於一個表中的行。大表名(由所有行組成)是kwf_uploads
,以許多名字出現的小表。我的目標是找到存在於kwf_uploads
中的行,但不存在於其他位置(這個例子是沒有關係的表中的主鍵和外鍵)。如何將表與多個工會進行比較並加入?
我在SQL做了什麼:
select id from
kwf_uploads
left join
(SELECT picture_id as id
FROM documents where picture_id is not null
UNION
SELECT file_id as id
FROM books where file_id is not null
UNION
SELECT picture_id as id
FROM employee where picture_id is not null
UNION
SELECT file_id as id
FROM flightFiles where file_id is not null
UNION
SELECT picture_id as id
FROM tasks where picture_id is not null
UNION
SELECT picture_id as id
FROM trainingContentQuestions where picture_id is not null
UNION
SELECT picture_id as id
FROM trainingQuestions where picture_id is not null) foo
ON kwf_uploads.id = foo.id
找到它:SQL: cascade UNION and JOIN
但它失敗,錯誤:SQL (1052): Column "id" in field list is ambiguous
。
我不想在每個表中使用連接,因爲我不擅長連接,並且sql變得非常大且不可讀。我也試過not exists
沒有任何結果。 我認爲,我們可以找到一個更好的解決方案=)
導致select id from kwf_uploads
查詢(9690行):
從聯合查詢結果(6096行):
結果我想看到3594(9690減去6096)行。
您的問題不是很清楚。向我們展示一些數據並期待結果。順便說一句,因爲你不擅長某事並不意味着你無法學習。 '加入'是最好的工具。 –
@JuanCarlosOropeza完成。 – Sogl
所以'上傳'='工會(文件,書籍,員工,飛行文件,任務,培訓內容問題,訓練問題)' –