簡體   English   中英

按不同的格式獲取Mysql組並計數結果

[英]Get Mysql group by and count result in a different format

我需要獲取每種狀態的計數。 所以我使用按狀態列分組並得到以下結果

+------------------+
|  Status  | count |
+------------------+
|  created |   2   | 
+------------------+
| verified |   1   | 
+------------------+

我想知道是否可以在一行中得到結果

+ ------------------+
| created | verified| 
+-------------------+
| 2       |   1     |
+-------------------+

在MySQL中,您可以執行

SELECT sum(status = 'created') as created,
       sum(status = 'verified') as verified
FROM your_table

對於所有其他數據庫引擎使用

SELECT sum(case when status = 'created' then 1 else 0 end) as created,
       sum(case when status = 'verified' then 1 else 0 end) as verified
FROM your_table

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM