问题解决了!
更新:
不太符合我的需要,让我们在带有字段 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 ;
对于这个(和 future 的)问题,在写问题时请记住:
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
关于php - 仅更新 MySQL 中重复条目的第一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620038/