1

Below is an over-simplified version of table I'm using:

fruits
+-------+---------+ 
| id    | type    | 
+-------+---------+ 
| 1     | apple   | 
| 2     | orange  | 
| 3     | banana  | 
| 4     | apple   | 
| 5     | apple   | 
| 6     | apple   | 
| 7     | orange  | 
| 8     | apple   | 
| 9     | apple   | 
| 10    | banana  | 
+-------+---------+ 

Following are the two queries of interest:

SELECT * FROM fruits WHERE type='apple' LIMIT 2;

SELECT COUNT(*) AS total FROM fruits WHERE type='apple'; // output 6

I want to combine these two queries so that the results looks like this:

+-------+---------+---------+  
| id    | type    | total   |
+-------+---------+---------+  
| 1     | apple   | 6       |
| 4     | apple   | 6       | 
+-------+---------+---------+

The output has to be limited to 2 records but it should also contain the total number of records of the type apple.

How can this be done with 1 query?

3
  • what's the purpose of such a query? Commented May 30, 2010 at 12:53
  • This is a trimmed down version of a table I'm using which will have more than a million records, and it's missing 5 other columns. I put this table for simplicity's sake. Right now I'm using 2 queries but wondered how this can be achieved with 1 query (for learning purpose) Commented May 30, 2010 at 13:00
  • Looks like you oversimplified it and it may mislead ones who will try to answer. Personally I prefer sensible questions. Commented May 30, 2010 at 13:03

2 Answers 2

2
SELECT *, (SELECT COUNT(*) AS total FROM fruits WHERE type='apple') AS Total 
FROM fruits WHERE type='apple' LIMIT 2;

Depending on how MySQL interprets it, it may cache the inner query so that it doesn't have to reevaluate it for every record.

Another way to do it is with a nested query and a join (this would be useful it you need more than one fruit type, for example):

SELECT fruits.*, counts.total
FROM fruits 
  INNER JOIN (SELECT type, COUNT(*) AS total FROM fruits GROUP BY type) counts ON (fruits.type = counts.type)
WHERE fruits.type='apple'
LIMIT 2;
Sign up to request clarification or add additional context in comments.

4 Comments

Hey @Lost, aren't your current 2 queries way simpler than this monster? ;)
@col: Well maybe. But I was curious to learn to do this with 1 query. And yes, though it may not look pretty, I prefer to use 1 query instead of 2.
@Lost and you'll be wrong. There is not a single benefit in doing it with one.
@col: hah, this query is a cake walk... take a look at one of these beasts I recently wrote: stackoverflow.com/questions/2938427/… stackoverflow.com/questions/2776632/…
1

You should use SQL_CALC_FOUND_ROWS for that.

SELECT SQL_CALC_FOUND_ROWS * FROM fruits WHERE type='apple' LIMIT 2;

will return the IDs of your apples, and remember how much it would have returned without the LIMIT clause

SELECT FOUND_ROWS();

will return how many apples would have been found, without the limit statement.

1 Comment

You should put the SQL_CALC_FOUND_ROWS in conditional-comments though, to allow graceful failing on SQL-servers that don't support it.

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.