tags:

views:

86

answers:

5

I'm not sure if this is even a good question or not.

I have a complex query with lot's of unions that searches multiple tables for a certain keyword (user input). All tables in which there is searched are related to the table book.

There is paging on the resultset using LIMIT, so there's always a maximum of 10 results that get withdrawn.

I want an extra column in the resultset displaying the total amount of results found however. I do not want to do this using a separate query. Is it possible to add a count() column to the resultset that counts every result found?

the output would look like this:

ID    Title    Author    Count(...)  
1     book_1   auth_1      23  
2     book_2   auth_2      23  
4     book_4   auth_..     23  

...

Thanks!

+2  A: 

The usual way of counting in a query is to group on the fields that are returned:

select ID, Title, Author, count(*) as Cnt
from ...
group by ID, Title, Author
order by Title
limit 1, 10

The Cnt column will contain the number of records in each group, i.e. for each title.

Guffa
<retracted comment>
Computer Guru
true that, but there is nothing to 'group by' and I need to count the total amount of rows, and not just the rows of the group...
Arsenal
@Arsenal: I see... If you want to return a count as well as the data that you are counting, then you need a separate query. You can make it a subquery or join it in to return it in the same result, but there is no way around it.
Guffa
Okay, I embrased the fact that I will need to use a second query, and it's working alright, speed is better than I thought. I'm not sure which answer to select as Correct Answer though (and I can't select Guffa's comment as an answer :-) )
Arsenal
A: 

You simply cannot do this, you'll have to use a second query.

ceteras
okay, I feared this was gonna be the case ...
Arsenal
+1  A: 

Regarding second query:

select tbl.id, tbl.title, tbl.author, x.cnt
from tbl
cross join (select count(*) as cnt from tbl) as x

If you will not join to other table(s):

select tbl.id, tbl.title, tbl.author, x.cnt
from tbl, (select count(*) as cnt from tbl) as x
Michael Buen
A: 

If your problem is simply the speed/cost of doing a second (complex) query I would suggest you simply select the resultset into a hash-table and then count the rows from there while returning, or even more efficiently use the rowcount of the previous resultset, then you do not even have to recount

Cobusve
+1  A: 

This won't add the count to each row, but one way to get the total count without running a second query is to run your first query using the SQL_CALC_FOUND_ROWS option and then select FOUND_ROWS(). This is sometimes useful if you want to know how many total results there are so you can calculate the page count.

Example:

select SQL_CALC_FOUND_ROWS ID, Title, Author
from yourtable
limit 0, 10;
SELECT FOUND_ROWS();

From the manual: http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_found-rows

Ike Walker
this actually did the trick, thanks!
Arsenal