[英]How to update a record by checking a value in another record in same table
[英]Update value of one record with the value of another record in the same table
我對你們有一個棘手的SQL問題。 我正在使用Drupal的Webform模塊。
該模塊的工作方式:
-網絡表單上的每個項目都是“ webform_submitted_data”表中的新記錄,
-sid =注冊用戶
-cid =網絡表單字段
表的結構:
NID | SID | CID | 否| 數據
1 --- 168 --- 1 --- 0 --- XXX
1 --- 168 --- 2 --- 0 --- YYY
問題:-CID 64應該使用相同SID的CID 56的值進行更新。
我認為應該如下:
UPDATE webform_submitted_data as a, webform_submitted_data as b
SET a.data = b.data WHERE a.sid = b.sid AND a.cid = 64 AND b.cid = 56
基本上,您可以自動聯接表(匹配SID),然后在查詢中同時擁有兩個字段,因此您可以一次簡單地引用每個字段。
對於每個SID值,這會將CID 56的數據復制到CID 64。 這是SQL Server語法。
update yt64
set Data = yt56.Data
from YourTable yt64
join YourTable yt56
on yt64.SID = yt56.SID
where yt64.CID = 64
and yt56.CID = 56
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.