2016-08-19 216 views
-2

MySQL的:如何在MySQL中使用group_concat()?

SELECT 
group_concat(cu1.cu_inflow_type),count(cu1.cu_inflow_type) 
FROM cs_test cu1 
LEFT JOIN cs_test cu2 
ON cu2.seq=cu1.seq 
GROUP BY cu1.cu_inflow_type 

结果:

====================================================================== 
group_concat(cu1.cu_inflow_type) | count(cu1.cu_inflow_type) 
---------------------------------------------------------------------- 
001,001,001,001,001,001,001,00.. | 27 
002,002        | 2 
003,003        | 2 
004,004,004       | 3 
005         | 1 
006         | 1 
---------------------------------------------------------------------- 

查询运行,但它不是我想要的结果。我想它看起来像:

====================================================================== 
group_concat(cu1.cu_inflow_type) | count(cu1.cu_inflow_type) 
---------------------------------------------------------------------- 
001,001,001,001,001,001,001,00.. | 27 
002,002,003,003      | 4    <-- add! 
004,004,004       | 3 
005,006        | 2    <-- add! 
---------------------------------------------------------------------- 

“002002” +“003003” = 002002003003 我不知道如何使用group_concat()在MySQL中得到期望的结果。有人能帮我吗?

+0

我对你想要的实际输出有点困惑。你能解释一下吗? – Takarii

+0

如果你需要WHERE子句 – Oncodeeater

回答

0

您可以使用小组中的其他字段

+0

,你可能想要在同一行中拥有同一个id的所有cu_inflow_type对不起,你能解释一下我怎样才能放SQL吗? –