我想知道如何写这个查询。
我知道这个实际的语法是虚假的,但它将帮助您理解我想要的东西。
我需要这种格式,因为它是一个更大查询的一部分。
SELECT distributor_id,
COUNT(*) AS TOTAL,
COUNT(*) WHERE level = 'exec',
COUNT(*) WHERE level = 'personal'
我需要在一个查询中返回这一切。
此外,它需要在一行中,所以下面的将不起作用:
'SELECT distributor_id, COUNT(*)
GROUP BY distributor_id'
我做了一些类似的事情,我只是给每个表一个字符串名称来在列a中标识它,并为列提供一个计数。然后我将它们合并,使它们堆叠。结果在我看来是漂亮的-不确定它与其他选项相比有多有效,但它给了我我需要的。
select 'table1', count (*) from table1
union select 'table2', count (*) from table2
union select 'table3', count (*) from table3
union select 'table4', count (*) from table4
union select 'table5', count (*) from table5
union select 'table6', count (*) from table6
union select 'table7', count (*) from table7;
结果:
-------------------
| String | Count |
-------------------
| table1 | 123 |
| table2 | 234 |
| table3 | 345 |
| table4 | 456 |
| table5 | 567 |
-------------------