繁体   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