Home > Database > Mysql Tutorial > body text

mysql合并多条记录的单个字段去一条记录编辑

WBOY
Release: 2016-06-07 18:05:35
Original
1079 people have browsed it

mysql怎么合并多条记录的单个字段去一条记录,今天在网上找了一下,方法如下

测试用表结构:
代码如下:
--
-- 表的结构 `tet`
--
CREATE TABLE IF NOT EXISTS `tet` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 转存表中的数据 `tet`
--
INSERT INTO `tet` (`id`, `name`, `url`) VALUES
(1, '百度 ', 'http://www.baidu.com '),
(0, 'google ', 'http://www.google.com.hk '),
(3, '400电话 ', 'http://www.jb51.net ');

方法一:
代码如下:
SELECT GROUP_CONCAT ( name ) name
FROM tet
WHERE 1 = 1
LIMIT 0 , 30

结果:
name 百度,google,400电话
GROUP_CONCAT还可以用 SEPARATOR 关键词指定连接符,sql语句如下:
SELECT GROUP_CONCAT ( url SEPARATOR " @ " ) url
FROM tet
WHERE 1 = 1
LIMIT 0 , 30
结果:
http://www.baidu.com@http://www.google.com.hk@http://www.jb51.net
方法二:
代码如下:
SELECT GROUP_CONCAT ( name ) name
FROM tet
WHERE 1 = 1
GROUP BY id
LIMIT 0 , 30

结果:
google
百度
400电话
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!