2010-06-01 67 views
0

我有两个表类别和酒店,其中category.id应该等于hotels.catid。现在,我应该如何从酒店表中的每个不同类别中选择3行。如何从表中选择每个不同类别的三行?

我有这个疑问:

select h.* from hotels h inner join category c on h.catid = c.id 
order by h.catid, h.hid 

这将选择所有记录,但我想每个选择不同类别三行所以在所有应该有3行对每个类别返回9行。

如果这不能在MySQL中完成,是否可以在PHP中完成?

+0

你想要3行?前3名?随机3?一个具体的3?最后3个? – AndrewC 2010-06-01 10:30:11

+0

@AndyC:顶部或末尾3,我想在每个类别的主页上显示3行。 – Sarfraz 2010-06-01 10:34:11

回答

4

试试这个:

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) 
       ) 
) 

我希望它的作品。如果没有,那么希望它能给你一些可能起作用的想法,或者甚至可以给别人一些关于如何解决这个问题的想法。至少它可以用来看看什么不起作用。

+0

未知列在“where子句” – Sarfraz 2010-06-01 10:56:10

+0

@ ME-和编码“c.id”:尝试更新的一个 – Senseful 2010-06-01 10:56:58

+0

哎呀,现在我得到这样的:该版本的MySQL还不支持“限制和IN/ALL/ANY/SOME subquery' – Sarfraz 2010-06-01 10:58:56

0

您应该能够使用DISTINCT(类别),并限制查询到3个结果

+0

我试过这个,但它只显示一只猫的记录:'SELECT DISTINCT(c.id),h。* FROM hotels h INNER JOIN category c ON h.catid = c.id ORDER BY h.catid,h.hid LIMIT 3' – Sarfraz 2010-06-01 10:33:18

0

所以,你要发现三个酒店每个类别......有选择!

我能想到的最简单的方法是执行每个类别的查询。

SELECT 
    h.* 
FROM 
    hotels h 
WHERE 
    h.catid = 1 
LIMIT 0, 3   
+0

你说h.catid = 1但catid不知道,它不可能是1,也可能是其他一些数字。 – Sarfraz 2010-06-01 10:34:39

0

您好我是不知道的查询,但我有一个备用way.first找到所有的类别阵列和发现从酒店表中的3条。

第一步: $ sql =“SELECT * FROM Table category”;
执行在类别阵列查询及地点结果

工序II: 做一个循环 对于($ i = 0; isset(类别阵列[$ I]); $ I ++) { 查询3记录从酒店TABEL 执行这一点,并在阵列 } 所有数据存储在这种方式,您还可以得到record.This是不是你的问题的确切解决方案,但你可以使用这个解决您的问题

2

你可以这样做它以下面的方式。

select 
    h.* 
from 
    hotels h 
where 
    h.catid IN (
     select 
      c.catid 
     from 
      category c 
     order by 
      RAND() 
     limit 1 
    ) 
order by 
    h.catid, h.hid 
limit 3 

这应该会给你3个酒店在一个随机类别。

用PHP,你可以做这样的

$i = 0; 
$sql = "select catid from category order by rand()"; 
$query = mysql_query($sql); 
while($row = mysql_fetch_object($query)) 
{ 
    if($i < 3) 
    { 
     $i++; 
     $categories[] = $row->catid; 
    } 
    else 
    { 
     break; 
    } 
} 

foreach($categories as $catid) 
{ 
    $i = 0; 
    $sql = "select * from hotels where catid = $catid"; 
    $query = mysql_query($sql); 
    while($row = mysql_fetch_object($query)) 
    { 
     if($i < 3) 
     { 
      $i++; 
      $hotels[] = $row; 
     } 
     else 
     { 
      break; 
     } 
    } 
} 
+0

该版本的MySQL还不支持“限制和IN/ALL/ANY/SOME子查询” – Sarfraz 2010-06-01 10:55:41

+0

更新我有一个PHP的解决方案的答复。 – Nalum 2010-06-01 11:47:42

+0

+1,我也是,但现在使用了我接受的查询唯一的解决方案。谢谢 – Sarfraz 2010-06-01 11:52:55

相关问题