Home > Database > Mysql Tutorial > 用SQL将查询出来的多列的值拼接成一个字符串_MySQL

用SQL将查询出来的多列的值拼接成一个字符串_MySQL

WBOY
Release: 2016-06-01 13:33:03
Original
1556 people have browsed it

bitsCN.com

用SQL将查询出来的多列的值拼接成一个字符串

 

MySQL中:

 

[sql] 

-- 单列拼接,先查出一行,再加上逗号,接着拼接 查出的下一行  

select group_concat(E.SUPPORT)  

from ENGINES E  

where E.XA IN('YES','NO')  

--结果如下:YES,YES,YES,YES,YES,YES,DEFAULT,YES  

  

-- 将查询的结果中的 "," 号,替换成 "**" 号   

select REPLACE(group_concat(E.SUPPORT),',','**')  

from ENGINES E  

where E.XA IN('YES','NO')  

--结果如下:YES**YES**YES**YES**YES**YES**DEFAULT**YES  

  

-- 将查询的结果中 重复 的记录 去掉,只剩一条记录,然后再拼接  

select group_concat(DISTINCT(E.SUPPORT))  

from ENGINES E  

where E.XA IN('YES','NO')  

--结果如下:YES,DEFAULT  

  

-- 多列拼接,同一行的先拼接,再加上逗号,接着拼接下一行  

select group_concat(E.ENGINE,E.SUPPORT)  

from ENGINES E  

where E.XA IN('YES','NO')  

--结果如下:MRG_MYISAMYES,MyISAMYES,BLACKHOLEYES,CSVYES,MEMORYYES,ARCHIVEYES,InnoDBDEFAULT,PERFORMANCE_SCHEMAYES  

 

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