开发者

How to select three rows from each different category from a table?

I have two tables category and hotels where category.id should be equal to hotels.catid. Now how do I select 3 rows from each different category from the hotels table.

I have this query:

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

This selects all records, but I want to select three rows per different category so in all it should return 9 rows with 3 rows for each category.

If this can not be done in 开发者_JAVA技巧MySQL, can it be done in PHP?


Try this:

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

I'm wondering if that is valid syntax in MySQL...

If not, you could try:

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)

3rd attempt:

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)

Alright here's another attempt which is just plain ugly:

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

I hope it works. If it doesn't though, hopefully it will give you ideas of something that might work, or maybe even give others ideas of how to solve this problem. At the very least it could be used to see what doesn't work.


You could do it in the following way.

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

This should give you 3 hotels in one random category.

With php you could do it like this

$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;
        }
    }
}


You should be able to use DISTINCT(category) and limit query to 3 results


So you want to find three hotels per category... there are options!

The easiest method I can think of is to perform the query per category.

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


hi i am not sure about the query but i have a alternate way.first find all the category array and the find the 3 record from the hotels tables.

step I: $sql="SELECT * FROM Table category ";
execute the query and place results in category array

Step II: Make a loop For($i=0;isset(category array [$i]);$i++) { query for 3 record from hotel tabel Execute this and store all data in array } In this way you can also get the record.This is not the exact solution of your problem but you can use this for resolving your problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜