问题解决了!
Update:
不太符合我的需要,让我们在带有字段 ID、NAME、COVER 的简单表上做示例
我有 100 个条目,其中有 100 个名称,其中一些名称重复,但我只想更新重复的第一个名称。
尝试更新数据库中所有重复项的所有第一行,真的很难做到,知道我该怎么做吗?下面是我尝试重建的代码,但此代码将所有重复项的每个第一个代码替换为最后一个代码。
架构,我希望它如何在下面工作
ID NAME COVER
1 Max 1
2 Max 0
3 Andy 1
4 Andy 0
5 Andy 0
UPDATE table t
JOIN (
SELECT MinID, b.Name LatestName
FROM table b
JOIN (
SELECT MIN(ID) MinID, MAX(ID) MaxID
FROM table
GROUP BY tag
HAVING COUNT(*) > 1
) g ON b.ID = g.MaxID
) rs ON t.ID = rs.MinID
SET t.Name = LatestName;
根本不清楚你想要什么。也许是这样:
UPDATE table AS t
JOIN
( SELECT MIN(ID) MinID
FROM table
GROUP BY Name
HAVING COUNT(*) > 1
) AS m
ON t.ID = m.MinID
SET t.Cover = 1 ;
对于这个(以及未来的)问题,当您编写问题时请记住:
1. a description of your problem, as clear as possible --- you have that
2. data you have now (a few rows of the tables) --- ok, nice
3. the code you have tried --- yeah, but better use same names
--- as the data and description above
4. the error you get (if you get an error) --- doesn't apply here
5. the result you want (the rows after the update in your case)
--- so we know what you mean in case we
--- haven't understood from all the rest
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)