PDA

View Full Version : Total rows in a table from a limited-result query


The Truth
11-16-2009, 12:46 AM
I hope my title isn't too confusing, but here is what I'd like, if possible.

Say I had a table called articles, and from the table articles I wanted to select the latest 20 articles, but also in the query find the total amount of rows in the articles table. Is something like this possible?

So the original query would be:

SELECT * FROM articles ORDER BY article_date DESC LIMIT 20


I'm trying to cut down as many queries as possible, which is why I ask.

Fou-Lu
11-16-2009, 06:15 AM
Use a nested query:

SELECT *, (SELECT count(*) FROM articles) AS article_count
FROM articles
ORDER BY article_date DESC
LIMIT 20


I left as wildcarded since the only field I know is the article_date. I also kept the same style with the field naming conventions.