如何在列中列出mysql聚合函数的项目

时间:2013-11-22 00:26:19

标签: php mysql

所以这是我的查询

SELECT 
bia_clients.name "Builder Name", 
bia_panels.value "Name on Panel", 
bia_sizes.size "Size", 
bia_panels.sign_id "Sign Locations", 
count(bia_panels.sign_id) AS "Number of Panels", 
revenue_sharing AS "Amount Per Panel", 
count(bia_panels.sign_id) * revenue_sharing "Return to City"

FROM bia_panels
JOIN bia_projects ON bia_panels.project_id = bia_projects.id
JOIN bia_clients ON bia_clients.id = bia_projects.client_id
JOIN bia_sizes on bia_sizes.id = bia_panels.size_id
JOIN bia_prices ON bia_prices.size_id=bia_sizes.id
WHERE LEFT(bia_panels.sign_id, 3) = 042
group by bia_panels.value with rollup;

此代码可以按预期方式获取我的列和总计 这里

Builder Name    |   Name on Panel   |   Size    |   Sign Locations  |   Number of Panels    |   Amount Per Panel    |   Return to City
______________________________________________________________________________________________________________________________________
HAMPTON INN     |   Hampton Inn     |   Single  |   042182          |           2           |           19.00       |       38.00
BIA             |   Model Homes     |   Single  |   042301N         |           1           |           19.00       |       19.00
Richmond        |   American        |   Single  |   042092          |           3           |           19.00       |       57.00
Standard Pacific|   Standard Pacific|   Single  |   042102          |           7           |           19.00       |       133.00
Standard Pacific|   NULL            |   Single  |   042102          |           13          |           19.00       |       247.00

我遇到的问题是获取Sign Locations字段我希望它列出构建该列总数的每个项目 例如,我会将我的列看起来像这样

... |   Sign Locations          |   Number of Panels    |   ...
..._________________________________________________...
    |   042182, 042117          |   2                   |
    |   042301N                 |   1                   |
    |   042092, 042097, 042005  |   3
    ...
    

就格式化而言,我可以使用一些COALESCE语句来清理空行我不担心

但我的争论是如何使Sign Locations字段列出所有位置

我试图使用嵌套的select语句(表中的SELECT id)但它仍然只返回第一个项

任何想法?

1 个答案:

答案 0 :(得分:0)

尝试使用GROUP_CONCAT()

来自文档:

  

<强> GROUP_CONCAT(表达式)

     

此函数返回带有连接非NULL的字符串结果   一组中的值。如果没有非NULL值,则返回NULL。

在你的情况下:

...
GROUP_CONCAT(bia_panels.sign_id) AS "Sign Locations", 
...