Jump to content

count / limit


astralaaron

Recommended Posts

Hi, i posted here: this topic about limiting 3 results from each different user who has products. I got it working, using 'UNION ALL' to join together the separate queries, limiting each one to 3.Now the problem that i am facing is i need to also return the total number of rows to display a "3 out of 10 products" type of thing. I would like to make it happen in the same query if possible. Or is it impossible?The query is along these lines:

(SELECT `tablea`.`field1`, `tablea`.`field2` FROM `tablea` WHERE `tablea`.`id`=1 LIMIT 0,3) UNION ALL (SELECT `tablea`.`field1`, `tablea`.`field2` FROM `tablea` WHERE `tablea`.`id`=2 LIMIT 0,3)

I need a count in there (EDIT: "in each select") which counts ALL not only the 3 from the LIMIT, can it be done?

Link to comment
Share on other sites

Couldn't you just add a count field in the field list? Have you tried that?
Well, i added 'count(*) as num'. And you are right it does count the total number. But only for the first set of them, and it retrieves 1 product from each user after adding that..Do you have any idea whats happening?
Link to comment
Share on other sites

i kind of see where there is a problem.. when there is 2 users, it UNION ALL's together 2 queries.. with 2 count functions. each of those queries returns a set of 3 results without the count function. but only 1 when it does the count. the count needs to be added to each result somehow

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...