在查询表

时间:2017-01-19 03:53:26

标签: mysql

我有4个不同的表:

username table:
| uid | username | user_type|
+-----+----------+----------+
| 1   | Aaron    | CT       |
| 2   | Ahmad    | FT       |
| 3   | Akir     | CT       |
| 4   | Adelyn   | FT       |
| 4   | Adeeb    | AP       |
| 4   | Aizad    | FT       |
| 4   | Adeline  | AP       |


proc_leader table:
| projectNo | process | proc_leader|
+-----------+---------+------------+
| 170001    | ANM BLD | Aaron      |
| 170001    | BGD COL | Ahmad      |

proc_checker table:
| projectNo | process | proc_checker |
+-----------+---------+--------------+
| 170001    | ANM BLD | Adeeb        |
| 170001    | ANM BLD | Adeline      |
| 170001    | BGD COL | Aizad        |

proc_staff table:
| projectNo | process | proc_staff |
+-----------+---------+------------+
| 170001    | ANM BLD | Aaron      |
| 170001    | ANM BLD | Adelyn     |
| 170001    | ANM BLD | Ahmad      |
| 170001    | BGD COL | Akir       |

然后我UNION所有表创建一个表并获得每个用户的user_type

SELECT a.*FROM(
 (
   SELECT * FROM proc_leader t1 LEFT JOIN(
     SELECT username, user_type FROM user GROUP BY username
   ) t2 ON t1.proc_leader = t2.username GROUP BY t1.proc_leader
 ) UNION(
   SELECT * FROM proc_checker t1 LEFT JOIN(
     SELECT username, user_type FROM user GROUP BY username
   ) t2 ON t1.proc_checker = t2.username GROUP BY t1.proc_checker
 ) UNION(
   SELECT * FROM proc_staff t1 LEFT JOIN(
     SELECT username, user_type FROM user GROUP BY username
   ) t2 ON t1.proc_staff = t2.username GROUP BY t1.proc_staff
 )
) AS a
ORDER BY a.projectNo

现在我的输出是:

| projectNo | process | proc_leader | user_type |
+-----------+---------+-------------+-----------+
| 170001    | ANM BLD | Adeeb       |    AP     |
| 170001    | ANM BLD | Adelyn      |    FT     |
| 170001    | BGD COL | Ahmad       |    FT     |
| 170001    | BGD COL | Aizad       |    FT     |
| 170001    | BGD COL | Akir        |    CT     |
| 170001    | ANM BLD | Aaron       |    CT     |
| 170001    | ANM BLD | Adeline     |    AP     |
| 170001    | BGD COL | Ahmad       |    FT     |

然后我想要做的是为每个进程计算user_type并将其放在一个单元格中。所以我希望我的输出是这样的:

| projectNo | process |     count         |
+-----------+---------+-------------------+
| 170001    | ANM BLD | 1(CT)+2(AP)+2(FT) |
| 170001    | BGD COL | 2(FT)+1(CT)       |

我知道我可以使用GROUP_COncat()将它放在一个单元格中,但是如何从收到的表中进行计算呢?

2 个答案:

答案 0 :(得分:1)

试试这个:

select 
    projectNo,
    process,
    GROUP_CONCAT(col separator '+') `count`
from (
SELECT
    projectNo,
    process,
    concat(count(*),'(',user_type,')') col
FROM(
 (
   SELECT * FROM proc_leader t1 LEFT JOIN(
     SELECT username, user_type FROM user GROUP BY username
   ) t2 ON t1.proc_leader = t2.username GROUP BY t1.proc_leader
 ) UNION(
   SELECT * FROM proc_checker t1 LEFT JOIN(
     SELECT username, user_type FROM user GROUP BY username
   ) t2 ON t1.proc_checker = t2.username GROUP BY t1.proc_checker
 ) UNION(
   SELECT * FROM proc_staff t1 LEFT JOIN(
     SELECT username, user_type FROM user GROUP BY username
   ) t2 ON t1.proc_staff = t2.username GROUP BY t1.proc_staff
 )
) AS a
group by projectNo, process, user_type
) t group by projectNo, process;

答案 1 :(得分:1)

LEFT JOIN中包含的每个表格都不需要UNION。相反,只需使用单个UNION子查询并将其连接到username表。

在下面的查询中,我会报告每个projectNoprocess组的所有三种用户类型,即使计数为零也是如此。如果您真的不希望用户类型在计数为零的情况下出现,那么您将需要做更多的工作。

SELECT t2.projectNo,
       t2.process,
       CONCAT(
           CAST(SUM(CASE WHEN t1.user_type = 'CT' THEN 1 ELSE 0 END) AS CHAR(50)), '(CT)+',
           CAST(SUM(CASE WHEN t1.user_type = 'AP' THEN 1 ELSE 0 END) AS CHAR(50)), '(AP)+',
           CAST(SUM(CASE WHEN t1.user_type = 'FT' THEN 1 ELSE 0 END) AS CHAR(50)), '(FT)')
           AS count
FROM username t1
INNER JOIN
(
    SELECT projectNo, process, proc_leader AS username
    FROM proc_leader
    UNION ALL
    SELECT projectNo, process, proc_checker
    FROM proc_checker
    UNION ALL
    SELECT projectNo, process, proc_staff
    FROM proc_staff
) t2
    ON t1.username = t2.username
GROUP BY t2.projectNo,
         t2.process

在这里演示:

SQLFiddle