![](/img/trans.png)
[英]Update all the records of a field in a table whose value is only in another table
[英]Struggling to UPDATE value in another table where all field of first table are certain value and first and other table share a field value
我有兩張桌子。 表1具有客戶信息表2具有預訂信息表1具有CustomerEmail作為主鍵,表2也具有CustomerEmail。
我正在嘗試制作一條SQL語句來更新表2中的字段值(例如P),其中表1的所有字段都具有特定值,表1的CustomerEmail等於表2。
我以前沒有使用過UPDATE,而是使用JOIN的新手。 目前,我已經嘗試過以下方法:
UPDATE Table 2 SET P='Yes'
LEFT JOIN Table 1 On Table2.CustomerEmail=Table1.CustomerEmail
WHERE Table1.value1='something1'
AND Table1.value2='something2' up to 14 values etc...
是與訂單有關還是我完全弄錯了?
您可以像這樣使用EXISTS():
UPDATE Table2
SET P='Yes'
WHERE EXISTS(select 1 from Table1
WHERE Table2.CustomerEmail=Table1.CustomerEmail
AND Table1.value1='something1'
AND Table1.value2='something2'
AND....)
這將檢查table1中是否存在具有相同id和所有列值的行,如果存在,它將更新該行。
我使用了sagi評論的exist函數,效果很好,謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.