代码之家  ›  专栏  ›  技术社区  ›  Sarfraz

如何从表中的每个不同类别中选择三行?

  •  0
  • Sarfraz  · 技术社区  · 14 年前

    我有两张表category和hotels,其中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
    

    这将选择所有记录,但我想为每个不同的类别选择三行,因此在所有记录中,它应该返回9行,每个类别有3行。

    如果不能在MySQL中完成,可以在PHP中完成吗?

    5 回复  |  直到 6 年前
        1
  •  4
  •   Senseful    14 年前

    试试这个:

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

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

        2
  •  2
  •   Nalum    14 年前

    你可以用下面的方法来做。

    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;
            }
        }
    }
    
        3
  •  0
  •   Ladislav    14 年前

    您应该能够使用distinct(category)并将查询限制为3个结果

        4
  •  0
  •   Fenton    14 年前

    所以你想在每个类别中找到三家酒店…有很多选择!

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

    SELECT
        h.* 
    FROM
        hotels h
    WHERE
        h.catid = 1
    LIMIT 0, 3        
    
        5
  •  0
  •   Badshah    14 年前

    嗨,我不确定这个查询,但我有另一种方法。首先查找所有类别数组,然后从酒店表中查找3条记录。

    第一步: $sql=“从表类别中选择*”;
    执行查询并将结果放入类别数组中

    第二步: 做一个循环 对于($i=0;ISset(类别数组[$i]);$i++) { 从酒店标签查询3条记录 执行此操作并将所有数据存储在数组中 } 这样,您也可以获得记录。这不是问题的确切解决方案,但您可以使用它来解决问题。