在mysql中将计数值显示为两个不同的列

时间:2014-08-14 13:39:41

标签: 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

sql fiddle http://sqlfiddle.com/#!2/faeb26/6

4 个答案:

答案 0 :(得分:1)

select sum(date between '2014-08-12' and '2014-08-14'), count(*) as totalcount from entries ;

SUM()中的布尔表达式等于true或false,1或0.因此,只需使用SUM()代替COUNT()

答案 1 :(得分:0)

将两个查询放在一起:

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';

答案 2 :(得分:0)

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)

答案 3 :(得分:0)

简单组合以导致其他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;

DEMO LINK