Combining 2 values ​​from two consecutive recordsets | MySQL

I want to get data from 1 SQL query table with two values

select c from tmp; c foo bar 2 rows in set (0.00 sec) 

The return data I need is & lt; foo, bar>

Concat does not, and I cannot find any string function. I can SUM integers from two lines. Why can't I also get string values?

+4
source share
2 answers

you can use the GROUP_CONCAT() function to get the values ​​together.

This will combine all string values ​​separated by comma

+2
source

Maybe you need group_concat() in MYSQL.

Noticing that in MYSQL you will need a sample:

* SQLFIDDLE demo

 Select department, group_concat(name,',') as nameList from foo group by department ; 

Results:

 Department NameList D1 John, Mary D2 Tim, Dan, Jack D3 Kate, Felix 

The following is the usage method in TSQL:

You can try the following sample code and configure it for your table / columns:

 SELECT department, namelist = STUFF( (SELECT ','+ Name FROM foo B WHERE b.department = a.department FOR XML PATH('')) , 1 , 1 , '' ) FROM foo A 

Or you can do CTE .

+1
source

All Articles