3

I have a query

SELECT 
count(*) as count 
from matches 
WHERE team1 = 9 
ORDER BY data DESC, id ASC LIMIT 10

The max result I should expect isn't supposed to be 10? I am getting 15 as result, am I doing something wrong?

4
  • try SELECT count(*) from matches WHERE team1 = 9 ORDER BY data DESC, id ASC LIMIT 10 Commented Nov 13, 2013 at 5:15
  • Is it possible to have an alias count? if it's a reserve word in mysql Commented Nov 13, 2013 at 5:16
  • It is fine to use count as a field alias. It's the name of a function, not actually a reserved word ("SELECT" is a reserved word). Commented Nov 13, 2013 at 5:18
  • @plain jane, still got 15 as result :\ Commented Nov 13, 2013 at 5:18

4 Answers 4

3

You need to do your limit in an inner select.

select count(*) from
(select * from matches WHERE team1 = 9 ORDER BY data DESC, id ASC LIMIT 10) ten_rows

sql fiddle

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

Comments

1

The LIMIT 10 clause applies after the SELECT ... part of the query. So it's counting the rows and putting that number into a single row before applying LIMIT 10.

If you were using the API and asking it how many rows there were in a resultset, then you would indeed get the reply of 10 from the query SELECT * FROM ... LIMIT 10.

Comments

1

You are just get the count values, limit is not supposed to work for you count rows.

Using LIMIT you will not limit the count or sum but only the returned rows.

In other words, It counts no.of records from table and returned as single row. Your select query matched rows count is 15.

Comments

0

Try to use this,

SELECT 
COUNT(*) AS COUNT 
FROM matches 
WHERE team1 = 9 
ORDER BY DATA DESC, id ASC 

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.