如何将多个数据组合成一行

时间:2015-10-07 17:10:19

标签: mysql

我在使用SELECT sql函数时遇到问题。我正在尝试从我的表中获取数据,如下所示:

 cid    | cname   | fid | fname          | type   | status  | 
 10     | sue     | 4   | Bun            | pickup | pending | 
 10     | sue     | 10  | Lamb           | pickup | pending | 
 10     | sue     | 17  | Cheese         | pickup | pending | 
 10     | sue     | 31  | Spicy          | pickup | pending | 
 12     | lho     | 5   | crispy bun     | pickup | pending | 
 12     | lho     | 8   | Chicken        | pickup | pending | 
 12     | lho     | 17  | Cheese         | pickup | pending | 
 12     | lho     | 29  | Normal         | pickup | pending | 

我试图这样做:

 cid   | cname   | fname                               | type   | status  |
 10    | sue     | Bun, Lamb, Cheese, Spicy            | pickup | pending |
 12    | lho     | Crispy bun, Chicken, Cheese, Normal | pickup | pending |

下面是我的查询:

$query="SELECT cid, cname, cnumber, type, date, fid, status, fname,
GROUP_CONCAT(fname SEPARATOR ', ') FROM purchase GROUP BY cname,cid";

我的问题有什么解决办法吗?请帮助并非常感谢你。

1 个答案:

答案 0 :(得分:0)

GROUP_CONCAT已为您添加了commas,无需添加,请参阅以下内容:

$query="SELECT cid, cname, cnumber, type, date, fid, status,
GROUP_CONCAT(fname) FROM purchase GROUP BY cname,cid";