![](/img/trans.png)
[英]Redshift SQL: Check if same set of values are IN and NOT IN two different columns of table
[英]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
我必須檢查asscid
即22297922
是否在實際存在的cid列中。 所以我必須比較asscid
在cid中的時間,然后在asscid
(其中cid
為253440
)的情況下獲得ci
的值(在這種情況下為1),然后將ci
的相同值1分配給cid
列22297922
,即在這種情況下為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.