当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql合并多条记录的单个字段去一条记录编辑

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

街头球球,成人 文学,陈美诗吻戏

测试用表结构:
复制代码 代码如下:

--
-- 表的结构 `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电话 ', '//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@//www.jb51.net
方法二:
复制代码 代码如下:

select group_concat ( name ) name
from tet
where 1 = 1
group by id
limit 0 , 30

结果:
google
百度
400电话

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网