Jump to content


Photo

PHP Selecting only 10 rows from each of multiple WHERE conditions


  • Please log in to reply
4 replies to this topic

#1 DillyDong

DillyDong
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 03:37 AM

Hi all, sorry, I know the title is ambiguous, but I wasn't sure exactly how to explain it.

Anyway,

Say I have the following code:

'SELECT * FROM `table` WHERE `list_number` IN (1,3,5)

Now, let's say that there are 20 rows for each matching `list_number` criteria (ie, There are 20 rows where `list_number` = 1, 20 rows where `list_number` = 3, and 20 rows where `list_number` = 5).

What I would like to know is, how would I tell the MySQL query to get only 10 (in order or random, doesn't matter) rows for each `list_number`? I thought that LIMIT only worked for the entire query, not per where condition.

Any help is appreciated! Thank you!



#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 04:06 AM

I don't think you can select a limit in a IN clause. You'll have to run 3 seperate queries I believe

#3 DillyDong

DillyDong
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 04:22 AM

Shucks. Using
WHERE (`list_number` = 1 OR `list_number` = 3 OR `list_number` = 5)
is also acceptable though, so any way to implement it with that sort of code?

#4 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 04:24 AM

Nope, the limit works on the full query, not a part of it. Just run the 3 queries and them the results of them into a single array if you want 1 result to show.

#5 DillyDong

DillyDong
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 02:01 PM

Alright, thanks for the reply anyway  :-\




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users