[英]displaying count values as two different columns in mysql
我在MySQL表中的數據具有以下格式。 我想在以下查詢中所示的兩種不同條件下檢索計數,我想將這些查詢合並為一個查詢,這意味着我希望第一個查詢結果在一個列中,第二個查詢結果在另一列中,這樣:
預期產量:
count totalcount
--------------------------
3 6
查詢:
select count(*) as count from entries where
date between '2014-08-12' and '2014-08-14';
select count(*) as totalcount from entries ;
mysql表中的數據:
id date
------------------------
1 2014-08-14
2 2014-08-13
3 2014-08-12
4 2014-08-11
5 2014-08-10
6 2014-08-09
select sum(date between '2014-08-12' and '2014-08-14'), count(*) as totalcount from entries ;
SUM()
的布爾表達式等於true或false,等於1或0。因此,只需使用SUM()
而不是COUNT()
。
只需將兩個查詢放在一起:
select count(*) as count, b.totalcount from entries,
(select count(*) as totalcount from entries) b
where date between '2014-08-12' and '2014-08-14';
select sum(c) as count, sum(tc) as totalcount
from (select count(*) as c , 0 as tc from entries where date between '2014-08-12' and '2014-08-14'
union all
select 0 as c, count(*) as tc from entries)
簡單結合以導致其他select
查詢嘗試
SELECT (select count(*) as count from entries where
date between '2014-08-12' and '2014-08-14'
) as count, (select count(*) as totalcount from entries) as totalcount;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.