Reputation: 9034
Is it possible to limit the results a mysql select query returns with a condition?
For example I have a reviews table:
review_id, member_id, text, date
And I'd like to get the latest 10 reviews but member_id = 123 should only be taken once
Can this be achieved with a single query?
Upvotes: 0
Views: 48
Reputation: 49105
My interpretation of the problem:
member_id
= 123I'm going to solve this by:
member_id
= 123 except for the most recent oneHere's the query:
create view newest123 as ( -- this gets the newest review for member_id 123
select *
from reviews
where member_id = 123
order by date desc limit 1
)
select *
from (
select * from newest123
union
select * -- all the reviews that aren't for member_id 123
from reviews
where member_id != 123) filtered
order by date desc limit 10 -- sort 'em and take the top 10
Upvotes: 4