how to get the number of rows that match a query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to get the number of rows that match a query

Post by jasongr »

Hello

I have a complicated query that uses LIMIT X,Y at the end
This causes the query to return up to Y element.
However I know that if the LIMIT was omitted, the query would have returned a much larger results set.

My quesion is this:
Is there a way of knowing how large the result set would have been without the LIMIT clause, without resorting to running two queries? one with the LIMIT (to get the items I need) and another without the LIMIT (to see how many items could potentially match the query)

thanks
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

don't think so

Post by phpScott »

Not that I'm aware off.
Maybe one of the php/mysql guru's around here tell you different.
Other wise sorry.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You'd need two queries.

A count() one, and a SELECT one.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Sami wrote:You'd need two queries.

A count() one, and a SELECT one.
Sami, you can't use count() for queries with GROUP BY clause this way. So, one SELECT without LIMIT followed by [php_man]mysql_num_rows[/php_man]() and then SELECT with LIMIT clause.
Post Reply