How to get multiple counts with one SQL query?

后端 未结 9 2171
清酒与你
清酒与你 2020-11-22 14:03

I am wondering how to write this query.

I know this actual syntax is bogus, but it will help you understand what I am wanting. I need it in this format, because it i

9条回答
  •  眼角桃花
    2020-11-22 14:28

    I do something like this where I just give each table a string name to identify it in column A, and a count for column. Then I union them all so they stack. The result is pretty in my opinion - not sure how efficient it is compared to other options but it got me what I needed.

    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;
    

    Result:

    -------------------
    | String  | Count |
    -------------------
    | table1  | 123   |
    | table2  | 234   |
    | table3  | 345   |
    | table4  | 456   |
    | table5  | 567   |
    -------------------
    

提交回复
热议问题