MYSQL Concat,Left Join,分组解决方案

Posted

技术标签:

【中文标题】MYSQL Concat,Left Join,分组解决方案【英文标题】:MYSQL Concat,Left Join,Group Solution 【发布时间】:2015-06-07 00:38:12 【问题描述】:

我有两张桌子

1) cawangan_tbl

 mysql> select * from cawangan_tbl
+------------+---------------------------+------------+-------+
| IdCawangan | NamaCawangan              | IdParlimen | IdDun |
+------------+---------------------------+------------+-------+
|          3 | Kampung Titi Tinggi Hilir |          1 |     6 |
|          6 | Kampung Titi Rendah Hilir |          3 |     6 |
|          7 | Kampung Titi Kiri Hilir   |          3 |     6 |
|          8 | Kampung Titi Kanan Hilir  |          3 |     6 |
+------------+---------------------------+------------+-------+            

2)minit_tbl

select IdMinit,IdDun,IdCawangan,IdParlimen,NoMinit,Status from minit_tbl
+---------+-------+------------+------------+---------+--------+
| IdMinit | IdDun | IdCawangan | IdParlimen | NoMinit | Status |
+---------+-------+------------+------------+---------+--------+
|      14 |     6 |          3 |          3 | 1       | OnTime |
|      15 |     6 |          3 |          3 | 2       | OnTime |
|      16 |     6 |          3 |          3 | 4       | Late   |
|      17 |     6 |          7 |          3 | 1       | Late   |
+---------+-------+------------+------------+---------+--------+

现在,我的输出是这样的:

select C.IdCawangan,
CONCAT(case when M.NoMinit = 1 then (CONCAT(M.NoMinit,'--> ',M.status)) else 0 end) MonthJan,
CONCAT(case when M.NoMinit = 2 then (CONCAT(M.NoMinit,'--> ',M.status)) else 0 end) MonthFeb,
sum(case when M.NoMinit = 3 then M.NoMinit else 0 end) MonthMac,
sum(case when M.NoMinit = 4 then M.NoMinit else 0 end) MonthApril,
sum(case when M.NoMinit = 5 then M.NoMinit else 0 end) MonthMei,
sum(case when M.NoMinit = 6 then M.NoMinit else 0 end) MonthJun
from cawangan_tbl C left join minit_tbl M 
on M.IdCawangan = C.IdCawangan
group by C.IdCawangan

+------------+-------------+----------+----------+------------+----------+----------+
| IdCawangan | MonthJan    | MonthFeb | MonthMac | MonthApril | MonthMei | MonthJun |
+------------+-------------+----------+----------+------------+----------+----------+
|          3 | 1--> OnTime | 0        |        0 |          4 |        0 |        0 |
|          6 | 0           | 0        |        0 |          0 |        0 |        0 |
|          7 | 1--> Late   | 0        |        0 |          0 |        0 |        0 |
|          8 | 0           | 0        |        0 |          0 |        0 |        0 |
+------------+-------------+----------+----------+------------+----------+----------+  

据推测,MonthFeb 的输出将是这样的:

+------------+-------------+-------------+----------+------------+----------+----------+
| IdCawangan | MonthJan    | MonthFeb    | MonthMac | MonthApril | MonthMei | MonthJun |
+------------+-------------+-------------+----------+------------+----------+----------+
|          3 | 1--> OnTime | 2--> OnTime |        0 |          4 |        0 |        0 |
|          6 | 0           | 0           |        0 |          0 |        0 |        0 |
|          7 | 1--> Late   | 0           |        0 |          0 |        0 |        0 |
|          8 | 0           | 0           |        0 |          0 |        0 |        0 |
+------------+-------------+-------------+----------+------------+----------+----------+      

有人可以指出正确的代码如何在下个月每个月显示MonthJan 吗?

【问题讨论】:

【参考方案1】:

在您的原始查询中添加了一些案例和聚合函数。

检查一下:

select C.IdCawangan,
coalesce(case when max(case when M.NoMinit = 1 then M.NoMinit else 0 end)>0 then 
    concat(max(case when M.NoMinit = 1 then M.NoMinit else 0 end),'-->',M.status) end,0)  MonthJan,
coalesce(case when max(case when M.NoMinit = 2 then M.NoMinit else 0 end) >0 then
    concat(max(case when M.NoMinit = 2 then M.NoMinit else 0 end),'-->',M.status) end,0) MonthFeb,
sum(case when M.NoMinit = 3 then M.NoMinit else 0 end) MonthMac,
sum(case when M.NoMinit = 4 then M.NoMinit else 0 end) MonthApril,
sum(case when M.NoMinit = 5 then M.NoMinit else 0 end) MonthMei,
sum(case when M.NoMinit = 6 then M.NoMinit else 0 end) MonthJun
from cawangan_tbl C left join minit_tbl M 
on M.IdCawangan = C.IdCawangan
group by C.IdCawangan;

希望得到帮助:)

【讨论】:

以上是关于MYSQL Concat,Left Join,分组解决方案的主要内容,如果未能解决你的问题,请参考以下文章

嵌套 CONCAT 中的 LEFT JOIN 和 IS NULL 仅返回 NULL

带有 LEFT JOIN 和 IF 语句的有限 GROUP_CONCAT

GROUP_CONCAT 与 LEFT JOIN 条件?

使用 GROUP_CONCAT 进行 LEFT JOIN 的奇怪结果

GROUP_CONCAT 与两个表的 LEFT OUTER JOIN

GROUP_CONCAT 和 LEFT_JOIN 问题 - 并非所有行都返回