我的名为simple_data
的表在名为MSISDN
的列中有数百万个手机号码,但它包含重复项。
所有行都将FLAG
列设置为零。
我想通过将FLAG
值设置为2
除一行来更新所有重复行。
CREATE TABLE `wt_simple_data` (
`SIMPLEID` int(10) NOT NULL AUTO_INCREMENT,
`CAMPID` bigint(20) NOT NULL,
`MSISDN` bigint(20) NOT NULL,
`FLAG` enum('0','1','2') DEFAULT '0' COMMENT '0 FOR NOT SENT, 1 FOR SENT',
PRIMARY KEY (`SIMPLEID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
一些示例数据:
SIMPLEID CAMPID MSISDN FLAG
15 311 9874123411 0
16 311 9874123411 0
17 311 9874123411 0
18 311 9874123412 0
19 311 9874123412 0
20 311 9874123413 0
21 311 9874123413 0
22 311 9874123414 0
我想要的结果
SIMPLEID CAMPID MSISDN FLAG
15 311 9874123411 0
16 311 9874123411 2
17 311 9874123411 2
18 311 9874123412 0
19 311 9874123412 2
20 311 9874123413 0
21 311 9874123413 2
22 311 9874123414 0
答案 0 :(得分:3)
您可以使用JOIN并将第一条记录保留为0
update
wt_simple_data w1
join (
select
min(SIMPLEID) as SIMPLEID,
CAMPID,
MSISDN
from wt_simple_data
group by CAMPID,MSISDN
)w2
on w1.SIMPLEID > w2.SIMPLEID
and w1.CAMPID = w2.CAMPID
and w1.MSISDN = w2.MSISDN
set w1.FLAG = '2'
答案 1 :(得分:0)
一般SQL:
UPDATE wt_simple_data
SET flag='2'
WHERE simpleid IN (
SELECT simpleid FROM wt_simple_data WHERE flag = '0'
MINUS
SELECT MIN(simpleid) FROM wt_simple_data WHERE flag = '0'
GROUP BY msisdn
)
使用分析功能:
UPDATE wt_simple_data
SET flag='2'
WHERE simpleid IN (
SELECT simpleid
FROM (
SELECT MIN(simpleid) OVER (PARTITION BY msisdn) minsimpleid, simpleid
FROM wt_simple_data
WHERE flag = '0')
WHERE minsimpleid <> simpleid
)
答案 2 :(得分:0)
UPDATE a
SET a.Flag = 2
FROM wt_simple_data a
INNER JOIN (SELECT MIN(SIMPLEID) SIMPLEID, CAMPID, MISSION
FROM wt_simple_data
GROUP BY CAMPID, MISSION
) ON a.CampId = b.CampId AND a.Mission = b.Mission AND a.SimpleId > b.SimpleId