微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 仅更新MySQL中重复条目的第一条记录

问题解决了!

更新:

不太正确我需要的东西,让我们在带有字段ID,NAME,COVER的简单表上做例子

我有100个条目有100个名字,一些名称是重复的,但我只想从重复更新第一个.

试图更新数据库中所有重复项的所有第1行,真的很难做到,我知道如何制作它?下面是我试图重建的代码,但是这个代码用最后一个代替所有重复代码.

架构,我希望它如何在下面工作

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 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐