Home > Database > Mysql Tutorial > mysql合并多条记录的单个字段去一条记录编辑_MySQL

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

WBOY
Release: 2016-06-01 13:22:18
Original
1047 people have browsed it

bitsCN.com 测试用表结构:

--
-- 表的结构 `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.bitsCN.com ');

方法一:

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.bitsCN.com
方法二:

SELECT GROUP_CONCAT ( name ) name
FROM tet
WHERE 1 = 1
GROUP BY id
LIMIT 0 , 30

结果:
google
百度
400电话bitsCN.com

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