使用MySql並需要從temp_comments中選擇(並稍後插入)數據到註釋中 - 除非註釋表中已存在註釋。下面是一個例子MySql加入2個沒有單個公共字段的表來排除記錄
comments table
id | apn | account | comment
--------------------------------
1 | 100 | | todays comment
2 | 101 | | yesterdays comment
3 | 109 | | todays comment
4 | 122 | | more comments
5 | | 34550 | todays comment
6 | | 12433 | another comment
temp_comments table
id | apn | account | comment
-----------------------------------------
1 | 92 | | todays comment
2 | 99 | | another comment
3 | 100 | | more comments
4 | 109 | | todays comment
5 | | 34588 | new comment
在這種情況下一些測試數據,所有除ID#4從temp_comments應選擇並插入註釋(因爲與記錄,APN |賬戶|評論所有匹配的行在評論表中)。
我試圖得到一個select語句試圖插入前的工作:
select apn, account, comment
from temp_comments
where not exists(select apn, account, comment
from temp_comments
except
select apn, account, comment
from comments)
因爲除非不MySQL的工作,這會彈出一個錯誤。不知道如何使用連接,因爲它的所有3個字段需要匹配我想要排除它。一旦我得到了SELECT工作,我需要插入註釋,如:
INSERT into comments
apn, account, comments
SELECT (and put my working SELECT statement HERE)
這實際上是從評論中獲得結果,而不是temp_comments。請注意我的編輯 - 我弄清楚如何在所有3個字段上使用CONCAT提取數據。 – pokerPro
對不起,我剛把它們弄倒了。 – Barmar
您應該將解決方案作爲答案發布,而不是編輯問題。 – Barmar