简体   繁体   English

来自多表和计数的mysQL group_concat

[英]mysQL group_concat from multi table and count

I tried to convert single table to multi table extraction single table which is working perfect but multi table extraction is giving error. 我试图将单表转换为多表提取单表,这是完美的工作,但多表提取给出了错误。 please any one can help 请任何人都可以帮忙

db-fiddle single table which is working perfect https://www.db-fiddle.com/f/mTHmv2idQwkdPZSqmRPi2Z/4 db-fiddle单表可以完美工作https://www.db-fiddle.com/f/mTHmv2idQwkdPZSqmRPi2Z/4

but whats wrong in this multi table i made??? 但是我做的这张多桌子怎么了? https://www.db-fiddle.com/f/eUAUt53neNMBsnP1QxjzGJ/5 https://www.db-fiddle.com/f/eUAUt53neNMBsnP1QxjzGJ/5

i expect below output same as fiddle i mention for single table. 我希望下面的输出与小提琴我提到的单表相同。 you can check the fiddle . 你可以检查小提琴。

|---------------------|------------------|
|      SELLER         |    status        | 
|---------------------|------------------|
|          S1         |C3 :3,C1 :2,C2 :2 | 
|---------------------|------------------|
|          S2         |C3 :1,C1 :2,C2 :1 |
|---------------------|------------------|

The reason you are getting more records than you are expecting is because of the multiple joins that you have in your query. 您获得比预期更多的记录的原因是由于查询中具有多个联接。

Try the following. 请尝试以下方法。 This should return to you just the: 这应该只返回给您:

select seller, group_concat(cid,' :', cnt  SEPARATOR ',') 
from
(SELECT  cases.SELLER, cases_cstm.customerid as cid, COUNT(*) as cnt FROM 
cases, cases_cstm WHERE cases.id=cases_cstm.id_c GROUP BY  cases.SELLER, 
cases_cstm.CUSTOMERID) q
group by seller;

If you need the count of the customer ids you should include count(cid) to your select clause. 如果需要计数客户ID,则应在您的select子句中包含count(cid) Hope this helps! 希望这可以帮助!

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

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