计算联合中的不同列并显示在同一行中

时间:2017-06-17 12:11:47

标签: mysql sql join union

我正在尝试使用count(*)从不同的表中获取不同列的union

//tbl_churidar

order_id    order_no_first    order_no
--------------------------------------
    1             C             1000
    2             C             1001
    3             C             1002

//tbl_anarkali

order_id    order_no_first    order_no
--------------------------------------
    1             A             1003
    2             A             1004
    3             A             1005

//tbl_assign

assign_id    order_id    order_no_first
---------------------------------------
   1            1              C
   2            1              A
   3            2              C
   4            3              C
   5            2              A
   6            3              A

//tbl_unit_status

status_id    assign_id    status_status    stitching_worker
-----------------------------------------------------------
    1           1            Stitch             AA
    2           2              QC             {null}
    3           3            Stitch             BB
    4           4            Stitch             BB
    5           5            Stitch             AA
    6           6            Stitch             CC

表格tbl_unit_status status_status = Stitch应该INNER JOIN与其他两个表格相同,并且churidaranarkali每个stitching_worker的总计数为churidar anarkali stitching_worker ---------------------------------------- 1 1 AA 2 0 BB 0 1 CC 服用。

所需的输出是,

SELECT churidar, anarkali, stitching_worker 
   FROM ((
          SELECT count(*) AS churidar, NULL AS anarkali, 
             us.stitching_worker 
          FROM tbl_unit_status us 
          INNER JOIN tbl_assign a ON a.assign_id = us.assign_id 
          INNER JOIN tbl_churidar o ON 
                (o.order_id = a.order_id AND 
                    o.order_no_first = a.order_no_first) 
          INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id 
          LEFT JOIN tbl_title t ON t.title_id = c.title_id 
          WHERE us.status_status = "Stitch" AND 
                   o.order_no_first = "C" 
          GROUP BY us.stitching_worker
         ) 
        UNION (
           SELECT NULL AS churidar, count(*) AS anarkali,
                     us.stitching_worker 
           FROM tbl_unit_status us 
           INNER JOIN tbl_assign a ON a.assign_id = us.assign_id 
           INNER JOIN tbl_anarkali o ON (
                       o.order_id = a.order_id AND 
                         o.order_no_first = a.order_no_first) 
           INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id  
           LEFT JOIN tbl_title t ON t.title_id = c.title_id 
           WHERE us.status_status = "Stitch" AND 
              o.order_no_first = "A" 
           GROUP BY us.stitching_worker
          )
         ) AS T1

我试图获得上述输出,但卡住了。以下是我的代码,

churidar    anarkali    stitching_worker
----------------------------------------
   1           0             AA
 {null}        1             AA
   2           0             BB
   0           1             CC

上述代码的输出是,

body{
   overflow: hidden;
}

如何获得所需的输出。我已经尝试了很多。帮我找到答案。三江源。

1 个答案:

答案 0 :(得分:2)

如果我理解正确(我可能没有),你不需要前两张表。您可以从tbl_assign获取所需信息,只需使用聚合:

select us.stitching_working,
       sum(a.order_no_first = 'C') as churidar,
       sum(a.order_no_first = 'A') as anarkali
from tbl_unit_status us join
     tbl_assign a
     on us.assign_id = a.assign_id 
where us.status_status = 'Stitch'
group by us.stitching_working;