我是mysql新手。我在插入記錄表1如果table2.I不存在問題已經在形式2個表Table 1和Table:如果表2中不存在如何插入表1?
table1
dep_id start stop modified deleted
1 23456789 167921525 Yes No
2 34567812 345678145 Yes No
3 32789054 327890546 No No
table2
start stop modified deleted
23456789 167921525 No No
34567823 345678145 No No
32789053 727890546 No No
我試圖將值插入表1的啓動且僅當停止字段值它不存在於table2的「開始」和「停止」列中。如果存在,我需要拋出一個錯誤。 這些表沒有主鍵外鍵關係。 我很抱歉不知道正確的語法,但我必須在mysql和PHP中做這樣的事情。
Replace Into into table1 set 'start'=> $start,'stop' => $stop
(select 'start','stop' from table2 where table1.start and table1.stop not in table2.start and table2.stop);
如何查詢這2個表插入到Table前檢查Table1.start和Table1.stop領域不Table2.start和Table2.stop匹配嗎?
我剛剛爲我的解決方案設置了一個SQLFiddle演示。嘗試一下。 – 2012-08-17 20:10:54