我的 table1
看起来像:
I need to increment
id name_co name_r temp sld 1 name1 1 ... ... 2 name2 1 ... ... 3 name2 1 ... ... 4 name2 1 ... ... 5 name3 1 ... ... 6 name2 1 ... ...
name_r
if there are two or more identical name_co
.
To be so:
id name_co name_r temp sld 1 name1 1 ... ... 2 name2 1 ... ... 3 name2 2 ... ... 4 name2 3 ... ... 5 name3 1 ... ... 6 name2 4 ... ...
I tried different options and I came to this:
UPDATE table1
SET name_r = name_r + 1
WHERE (SELECT COUNT(*)
GROUP BY name_co
HAVING name_co > 1)
查询有效并返回 0 行,但我知道在某种程度上他错了,但我不知道是什么。谁能帮忙? (还有一些解释,让我更好地理解)
最佳答案
--updated intended targets
UPDATE table1
SET name_r = name_r + 1
WHERE id IN
(
-- return those ids again (to avoid the mysql #1093 error)
SELECT id
FROM
( -- get all the ids for those names
SELECT id
FROM table1
WHERE name_co IN
( -- get all names that have more than one id
SELECT name_co
FROM table1
GROUP BY name_co
HAVING COUNT(id) > 1
)
) a
)
关于MySql如何使用UPDATE和HAVING?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42261038/