Mysql 带有join和';其中';连接表上的语句

Mysql 带有join和';其中';连接表上的语句,mysql,join,subquery,Mysql,Join,Subquery,有两个表,q\u条目(80条记录)和相关的q\u条目(37条记录) 当我加入时: select q_entries.*,q_entries_comments.comment from q_entries left join q_entries_comments on q_entries_comments.entry_id=q_entries.id 我有109张唱片 现在,我想将结果限制为仅10个条目,首先我要执行以下操作: select q_entries.*,q_entries_co

有两个表,
q\u条目(80条记录)和相关的
q\u条目(37条记录)

当我加入时:

select q_entries.*,q_entries_comments.comment  
from q_entries 
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id
我有109张唱片

现在,我想将结果限制为仅10个条目,首先我要执行以下操作:

select q_entries.*,q_entries_comments.comment  
from q_entries 
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
limit 0,10
select q_entries.*,q_entries_comments.comment  
from (select * from q_entries limit 0,10) as q_entries
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
但这并不是我想要的——在结果集中有一个id=1的重复条目,在这个“条目”上附加了10条注释

我想要的是得到10个不同的条目,每个条目有多少条评论,所以我这样做:

select q_entries.*,q_entries_comments.comment  
from q_entries 
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
limit 0,10
select q_entries.*,q_entries_comments.comment  
from (select * from q_entries limit 0,10) as q_entries
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
现在我得到了我想要的,那就是37条记录(前10条“条目”有这么多注释),但只有10条第一条“条目”

当我向相关表中添加
where
语句时,问题就出现了,如下所示:

select q_entries.*,q_entries_comments.comment  
from (select * from q_entries limit 0,10) as q_entries
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
where q_entries_comments.comment like '%b%'
select q_entries.*,q_entries_comments.comment  
from (select * from q_entries limit 0,10) as q_entries
left join (select * from q_entries_comments where q_entries_comments.comment like '%b%') as q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
它显然显示的“条目”太少,因为它只在附加到前10个“条目”的注释中搜索“%b%”。我真正想要的是总是得到10个“条目”和所有附加的评论


如何执行此操作?

您可以在第二个内部查询中使用WHERE子句,如下所示:

select q_entries.*,q_entries_comments.comment  
from (select * from q_entries limit 0,10) as q_entries
left join q_entries_comments 
on q_entries_comments.entry_id=q_entries.id 
where q_entries_comments.comment like '%b%'
select q_entries.*,q_entries_comments.comment  
from (select * from q_entries limit 0,10) as q_entries
left join (select * from q_entries_comments where q_entries_comments.comment like '%b%') as q_entries_comments 
on q_entries_comments.entry_id=q_entries.id