0

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

4 Answers 4

1
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().

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.