简体   繁体   中英

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

I have to check whether asscid ie 22297922 is in the cid column which is there actually. So I have to compare when the asscid is in the cid and then get the value of ci in case of asscid (where cid is 253440 )which is 1 in this case and then assign the same value 1 of ci to cid column 22297922 which is null in this case.

Use an update with a self join.

Select:

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:

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

Update with condition: (So that only rows that have different ci are updated)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM