Page 1 of 1
Getting total number of matches while limiting return.
Posted: Thu Dec 20, 2007 6:39 pm
by arpowers
Hey guys,
What is the easiest way to limit the number of results returned by a query (e.g. limit 20...) while returning the total number of database matches.. (e.g. john has 28 friends... )
in that case you wouldn't want to show all returned results.
thanks
Andrew
Posted: Thu Dec 20, 2007 6:55 pm
by seppo0010
I think
found_rows() is the easier way.
I´m not sure if it works in a SQL besides MySQL.
Posted: Thu Dec 20, 2007 7:15 pm
by arpowers
exactly exactly what I was looking for!
You are the best!
Thanks:D
Andrew
Posted: Fri Dec 21, 2007 3:38 am
by onion2k
Wow.. I didn't know that existed. Very useful indeed.
Posted: Fri Dec 21, 2007 4:21 am
by s.dot
Yeah, I didn't know about that either. Usually I would've ran two separate queries to get that information, or made some clever use of count().
Posted: Fri Dec 21, 2007 5:06 am
by Inkyskin
Me too... this is going to help my pagination class no end

Posted: Fri Dec 21, 2007 8:20 am
by VladSun
I read in "MySQL vs. PGSL" forums that using SQL_CALC_FOUND_ROWS/FOUND_ROWS(), indeed makes MySQL engine execute two separate queries, so it's not so good as it sounds

Just keep in mind about this and performance
Posted: Fri Dec 21, 2007 10:10 am
by onion2k
VladSun wrote:I read in "MySQL vs. PGSL" forums that using SQL_CALC_FOUND_ROWS/FOUND_ROWS(), indeed makes MySQL engine execute two separate queries, so it's not so good as it sounds

Just keep in mind about this and performance
True I guess, but it'd only have to parse and optimise the query once and it might do other internal optimisations like storing information about the first 'pass' so it'd still be much quicker than doing the query twice yourself.