试试这个:
select h.*
from category c
inner join (select * from hotels h where c.id = h.catid limit 3) h
order by h.catid, h.hid
我想知道,如果这是在MySQL中有效的语法......
如果没有,你可以尝试:
select h.*
from category c
inner join hotels h on (c.id = h.catid)
where h.hid in (select h2.hid from hotels h2 where h2.catid = c.id limit 3)
第三尝试:
select h.*
from category c
inner join hotels h on (c.id = h.catid)
where exists (select * from (select h2.hid from hotels h2 where h2.catid = c.id limit 3) h3 where h3.hid = h.hid)
好吧,这里是另一种尝试这只是难看:
select * from hotels h
where h.hid <=
(select min(h1.hid)
from hotels h1
where h1.catid = h.catid
and h1.hid > (select min(h2.hid)
from hotels h2
where h2.catid = h1.catid
and h2.hid > (select min(h3.hid)
from hotels h3
where h3.catid = h2.catid)
)
)
我希望它的作品。如果没有,那么希望它能给你一些可能起作用的想法,或者甚至可以给别人一些关于如何解决这个问题的想法。至少它可以用来看看什么不起作用。
你想要3行?前3名?随机3?一个具体的3?最后3个? – AndrewC 2010-06-01 10:30:11
@AndyC:顶部或末尾3,我想在每个类别的主页上显示3行。 – Sarfraz 2010-06-01 10:34:11