GROUP BY多个表MYSQL

时间:2019-04-05 09:13:19

标签: mysql

我有三个要在MySQL中查询的表,如下所示:

餐桌员工

id  | name_employee
-------------------
1   | John
2   | David
3   | Sara
4   | Tina

表格状态

id  | name_status
-----------
1   | no process
2   | processing
3   | processed

餐桌票

id  |  id_employee  |  id_status
----------------------------------
1   |        1      |     2
2   |        2      |     2
3   |        2      |     3
4   |        3      |     3
5   |        1      |     1

我需要查询结果如下:

name_employee  |  count(ticket.id) where id_status = 1 | count(ticket.id) where id_status = 2 | count(ticket.id) where id_status = 3
-----------------------------------------------------------------------------------------------------------------------------------------
David          |          0                            |        1                             |     1   
John           |          1                            |        1                             |     0
Sara           |          0                            |        0                             |     1

任何帮助,不胜感激!非常感谢!

1 个答案:

答案 0 :(得分:1)

使用可以在表达时使用大小写

select created from my_table;
            created
-------------------------------
 2019-02-03 10:05:40.462164+02