MYSQL-将具有多个重复值的行合并,然后删除重复项
作者:互联网
因此,我将数据库设置为单个表.在该表中,我收集了源URL和描述(我从许多页面上抓取了产品描述).不幸的是,如果有多个段落,那么我最终在数据库中为URL /源页面添加了多行.
我想做的是,如果有多个具有相同URL的行,请合并每行的描述,然后删除该URL的重复行.
我的表的字面结构如下:
table
+----+----------------------------+-------------+
| id | url | description |
+----+----------------------------+-------------+
| 1 | http://example.com/page-a | paragraph 1 |
| 2 | http://example.com/page-a | paragraph 2 |
| 3 | http://example.com/page-a | paragraph 3 |
| 4 | http://example.com/page-b | paragraph 1 |
| 5 | http://example.com/page-b | paragraph 2 |
+----+----------------------------+-------------+
我想要的是这样的:
table
+----+----------------------------+-------------------------------------+
| id | url | description |
+----+----------------------------+-------------------------------------+
| 1 | http://example.com/page-a | paragraph 1 paragraph 2 paragraph 3 |
| 2 | http://example.com/page-b | paragraph 1 paragraph 2 |
+----+----------------------------+-------------------------------------+
我对ID的更新正确性并不感到烦恼,我只想能够合并段落应位于与URL相同的相同字段中的行,然后删除重复项.
任何帮助将不胜感激!
解决方法:
过滤表很容易,只需将结果插入新表即可:
SELECT url, GROUP_CONCAT(description ORDER BY description SEPARATOR ' ') AS description
FROM `table`
GROUP BY url
标签:concat,group-concat,sql,mysql 来源: https://codeday.me/bug/20191120/2040919.html