是否可以使用条件限制mysql select查询返回的结果?
例如,我有一个评论表:
review_id,member_id,text,date
我想获得最新的10条评论,但是member_id = 123应该只进行一次
这可以通过一次查询实现吗?
答案 0 :(得分:4)
我对问题的解释:
member_id
= 123 我将通过以下方式解决这个问题:
member_id
= 123 的评论,但最新评论除外 以下是查询:
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