Macbernie Macbernie - 5 months ago 10
SQL Question

Select count(field) and select field, is that count on the same lines?

If I make a COUNT on the fields of mytable with a LIMIT parameter,

SELECT COUNT(field1), COUNT(field2)
FROM mytable
LIMIT 10000


And if I select there fields

SELECT field1, field2
FROM mytable
LIMIT 10000


Are there two requests select and count on the same 10000 lines ?

Because I can add a ORDER BY clause two the second request, but for a COUNT request it is not possible.

Thanks for help

Answer

Don't count on it, without an ORDER BY clause to specify the wanted order, there are no guarantees !

Just wrap the first query with another select :

SELECT COUNT(field1),count(field2) 
FROM(SELECT field1,field2
     FROM mytable
     ORDER BY Order_Column
     LIMIT 10000) t

SELECT field1, field2
FROM mytable
ORDER BY Order_Column
LIMIT 10000

These queries will process the same rows

Comments