user3791078
user3791078

Reputation: 69

displaying count values as two different columns in mysql

I have data in a MySQL table in the following format. I want to retrieve the count in two different conditions as shown in the query below, I want to combine these queries into a single one, by which I mean I would like the first query result in one column and second query result in another column, as so:

Expected output:

   count      totalcount
   --------------------------
   3          6

Queries:

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

 select count(*) as totalcount from entries ;

Data in mysql table:

  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

Upvotes: 0

Views: 48

Answers (4)

Girish
Girish

Reputation: 12127

simple combine to result in on other select query try this

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

Upvotes: 0

Conffusion
Conffusion

Reputation: 4475

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)

Upvotes: 0

LHristov
LHristov

Reputation: 1123

Just put the two queries together:

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

Upvotes: 0

fancyPants
fancyPants

Reputation: 51888

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

The boolean expression in SUM() equals to true or false, 1 or 0. Therefore just use SUM() instead of COUNT().

Upvotes: 1

Related Questions