简体   繁体   English

如何编写查询以通过逗号将不同行的列值分隔为php中的单行

[英]How to write a query to get different row's column value by comma separated into a single row in php

after executing this query: 执行此查询后:

`select A.idrequirement , A.title  , A.location , A.companyname,
 A.createdby, B.assignto from " . $_schema . ".requirement A, 
req_assignto_link B where A.createdby = '".$user."' and 
A.idrequirement=B.sourceid order by A.createdon`  ;

I got something like this: 我得到这样的东西:

idrequirement title location companyname createdby assignto idrequirement标题位置公司名称由Assignto创建

25         Android   Banglore  Barclays     mangesh    ninad 
25         Android   Banglore  Barclays     mangesh    mangesh

but I want something like this, 但我想要这样的东西

idrequirement | idrequirement | title | 标题| location | 位置| companyname | 公司名称| createdby | 创建者| assignto 分配给

25         | Android | Banglore  | Barclays  | mangesh   | ninad,mangesh

can anyone please tell how to do this? 谁能告诉我该怎么做?

We can use MySQL's GROUP_CONCAT() for this purpose: 为此,我们可以使用MySQL的GROUP_CONCAT()

SELECT idrequirement,
       title,
       location,
       companyname,
       createdby,
       GROUP_CONCAT(assignto) AS assignto
FROM yourTable
GROUP BY idrequirement,
         title,
         location,
         companyname,
         createdby

GROUP_CONCAT() will aggregate all values of the column specified as input using CSV format, for each group in the query. 对于查询中的每个组, GROUP_CONCAT()将使用CSV格式聚合指定为输入的列的所有值。

It will be something like this 会是这样的

"select A.idrequirement , A.title , A.location , A.companyname, A.createdby,
GROUP_CONCAT(B.assignto) AS assignto 
FROM " . $_schema . ".requirement A, req_assignto_link B
WHERE A.createdby = '".$user."' and A.idrequirement=B.sourceid
GROUP BY A.idrequirement ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM