簡體   English   中英

檢查同一表的兩個不同列中的相同值,然后在sql中進行比較

[英]Checking the same values in two different columns of same table and then compare in sql

cid       ctypeid  tid   check   asscid      ci
19149     6        2     0      NULL         0
253440    1        1     0      22297922     1
1361285   5        2     0      NULL         1
22297922  2        1     1      NULL         NULL
49821961  5        1     0      NULL         1

我必須檢查asscid22297922是否在實際存在的cid列中。 所以我必須比較asscid在cid中的時間,然后在asscid (其中cid253440 )的情況下獲得ci的值(在這種情況下為1),然后將ci的相同值1分配給cid22297922 ,即在這種情況下為null

通過自我聯接使用更新。

選擇:

SELECT t.asscid, t2.cid, t.ci, t2.ci --We will next update t2.ci with t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid

更新:

UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid

使用條件更新:(以便僅更新具有不同ci行)

UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
              and t.ci <> t2.ci

從emp A中選擇Assid(如果存在)(從emp B中選擇1,其中a.assid = b.Cis)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM