P粉2014488982023-08-27 14:25:34
这似乎有效,但我相信还有一种更优雅的解决方案,不需要使用join...
SELECT * FROM cus ; update cus inner join ( select id ,email,row_number() over(order by id asc) as rowid from cus )a on a.id = cus.id set cus.email = concat(a.rowid, a.email) ; SELECT * FROM cus ;
完整测试
-- 创建表 CREATE TABLE cus ( id INTEGER PRIMARY KEY, email TEXT NOT NULL ); -- 插入数据 INSERT INTO cus VALUES (0021, 'Clark'); INSERT INTO cus VALUES (0402, 'Dave'); INSERT INTO cus VALUES (005, 'Ava' ); SELECT * FROM cus ; update cus inner join ( select id ,email,row_number() over(order by id asc) as rowid from cus )a on a.id = cus.id set cus.email = concat(a.rowid, a.email) ; SELECT * FROM cus ;
P粉8850351142023-08-27 00:04:35
也许这就是你想要做的:
WITH C AS ( SELECT email,ROW_NUMBER() OVER(ORDER BY id ASC) AS rowid FROM cus ) UPDATE cus JOIN C ON cus.email=C.email SET cus.email=CONCAT(rowid,'email@gmail.com');
将你想要更新的表(cus
)与C
的cte
进行连接,然后进行相应的更新。
@QisM对于email
不唯一时的语法提出了疑虑,由于OP没有提到,我同意如果email
确实不唯一,这不是解决方案。因此,我对语法进行了轻微修改:
WITH C AS ( SELECT id, email, ROW_NUMBER() OVER(ORDER BY id ASC) AS rowid FROM cus ) UPDATE cus JOIN C ON cus.id=C.id AND cus.email=C.email SET cus.email=CONCAT(rowid,'email@gmail.com');
现在cte
带有id
,并且在JOIN C ON ..
条件中,我添加了匹配id
的检查。经过测试,如果电子邮件不唯一,这将修复该问题。