将2个值组合到1列MySQL

时间:2016-07-28 03:21:28

标签: mysql

代码:

SELECT s.*, k.address FROM student s
INNER JOIN address k
ON instr(s.fulladdress, k.address) > 0
WHERE j.phonetic like '%california%' 

此结果将显示2行,包含2部分地址:

30,Andre Hans, Address 1 Address 2,class IBA1001, Address 1

30,Andre Hans, Address 1 Address 2,class IBA1001, Address 2

我想将它显示在一行中:

30,Andre Hans, Address 1 Address 2,class IBA1001, Address 1 | Address 2

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

使用group bygroup_concat

SELECT s.*, group_concat(k.address separator ' | ') FROM student s
INNER JOIN address k
ON instr(s.fulladdress, k.address) > 0
WHERE j.phonetic like '%california%' 
GROUP BY s.col1, s.col2 ....  -- all the columns in table student