你不能做到这一点是这样的:你的GROUP BY
不要随你SET CONCAT
去。
这里是如何做到这一点:
UPDATE `plants`
SET `common_name2` = CONCAT(`common_name`, ' (', `genus`, ' ', `species`, ')')
WHERE `common_name` IN (
select `common_name`
FROM (select * from `plants`) plants
WHERE `common_name` != ''
GROUP BY `common_name`
HAVING COUNT(`common_name`) > 1
)
SQL Fiddle
的MySQL 5.6架构设置:
CREATE TABLE plants
(`id` int, `common_name` varchar(50), `common_name2` varchar(50), `genus` varchar(9), `species` varchar(12))
;
INSERT INTO plants
(`id`, `common_name`, `common_name2`, `genus`, `species`)
VALUES
(1, 'Roses', NULL, 'Rosa', 'Hulthemia'),
(2, 'Roses', NULL, 'Rosa', 'Hesperrhodos'),
(3, 'Roses', NULL, 'Rosa', 'Platyrhodon'),
(4, 'Roses', NULL, 'Rosa', 'Rosa'),
(5, 'Petunia', NULL, 'Petunia', 'axillaris'),
(6, 'Petunia', NULL, 'Petunia', 'integrifolia'),
(7, 'Cardinal', NULL, 'Lobelia', 'cardinalis'),
(8, 'Anthurium', NULL, 'Anthurium', 'andraeanum')
;
UPDATE `plants`
SET `common_name2` = CONCAT(`common_name`, ' (', `genus`, ' ', `species`, ')')
WHERE `common_name` IN (
select `common_name`
FROM (select * from `plants`) plants
WHERE `common_name` != ''
GROUP BY `common_name`
HAVING COUNT(`common_name`) > 1
)
查询1:
select * from plants
Results:
| id | common_name | common_name2 | genus | species |
|----|-------------|--------------------------------|-----------|--------------|
| 1 | Roses | Roses (Rosa Hulthemia) | Rosa | Hulthemia |
| 2 | Roses | Roses (Rosa Hesperrhodos) | Rosa | Hesperrhodos |
| 3 | Roses | Roses (Rosa Platyrhodon) | Rosa | Platyrhodon |
| 4 | Roses | Roses (Rosa Rosa) | Rosa | Rosa |
| 5 | Petunia | Petunia (Petunia axillaris) | Petunia | axillaris |
| 6 | Petunia | Petunia (Petunia integrifolia) | Petunia | integrifolia |
| 7 | Cardinal | (null) | Lobelia | cardinalis |
| 8 | Anthurium | (null) | Anthurium | andraeanum |
只是一个建议。您可以尝试使用http://sqlfiddle.com/创建一个包含一些示例数据的SQL小提琴。对其他人来说会更容易。 – kkaosninja