6

テーブルカテゴリ(idcat_name)とテーブルレシピ(、、)がidありcat_idますrecipe_text

次に、各カテゴリから10個のレシピをフェッチするクエリを作成します。

SELECT cat_name, recipe_text 
FROM categories c 
JOIN recipes r ON c.id=r.cat_id

すべてのレシピを取得しますが、カテゴリごとに最大10個のレシピが必要です。

(どのように)それはSQLクエリで行うことができますか?

4

6 に答える 6

1

mySQLから取得 各カテゴリの上位 5 を返す:

SELECT cat_name, recipe_text
FROM
(
   SELECT c.cat_name AS cat_name, r.recipe_text AS recipe_text,
      @r:=case when @g=c.id then @r+1 else 1 end r,
      @g:=c.id
   FROM (select @g:=null,@r:=0) n
   CROSS JOIN categories c
   JOIN recipes r ON c.id = r.cat_id
) X
WHERE r <= 10
于 2012-12-20T12:57:13.993 に答える
0

最終的にストアドプロシージャを作成しました。

DELIMITER $$

CREATE PROCEDURE `test`()
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE cur_id INT;
  DECLARE cur CURSOR FOR
  SELECT id FROM category;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  CREATE TABLE tmp(catname VARCHAR(255), recipeId INT);

  OPEN cur;

  the_loop: LOOP 
    FETCH cur INTO cur_id;

    IF done THEN
      LEAVE the_loop;
    END IF;

    INSERT INTO tmp(catname, recipeId) 
      SELECT catname, recipeId 
      FROM category c LEFT JOIN recipes r ON c.id=r.cat_Id 
      WHERE c.id=cur_id
      LIMIT 0, 10;
  END LOOP;

  SELECT * FROM tmp;
  DROP TABLE tmp;
END
于 2012-12-20T12:01:32.877 に答える
0

以下のSQLを試してください

select 
cat_name,
recipe_text 
from categories as c,recipes as r 
where c.id=r.cat_id limit 10
于 2012-12-20T10:57:30.323 に答える
0
SET @num :=0, @cat_id := '';

SELECT recipe_text,cat_name,
       @num := if(@cat_id = cat_id, @num + 1, 1) as row_number,
       @cat_id := cat_id as cat_id
FROM recipes r
JOIN categories c ON c.id = r.cat_id
group by cat_name,recipe_text
having row_number <= 10
于 2012-12-20T12:58:55.023 に答える
0

以下のコードを試すことができます

function recipe($cat_id='',$new_ar=array())
{
    if($cat_id!="")
    {
        $new_ar1=array();
        $db="select recipe_text from recipes where cat_id=".$cat_id." order by cat_id limit 10";
        $sq=mysql_query($db);
        while($fe=mysql_fetch_object($sq))
        {
            array_push($new_ar1,$fe->recipe_text);
        }
        return $new_ar1;
    }
    else
    {
        $db="select id,cat_name from categories order by id";
        $sq=mysql_query($db);
        while($fe=mysql_fetch_object($sq))
        {
            array_push($new_ar,$fe->cat_name);

            $new_ar[$fe->id]=array($fe->cat_name);

            array_push($new_ar[$fe->id],recipe($fe->id,$new_ar));
        }
    }

}

recipe();

it will give output like below



Array
(
    [0] => cat 1
    [1] => Array
        (
            [0] => cat 1
            [1] => Array
                (
                    [0] => rice
                    [1] => curry
                    [2] => mutton
                    [3] => A recipe
                    [4] => B recipe
                    [5] => C recipe
                    [6] => D recipe
                    [7] => E recipe
                    [8] => F recipe
                    [9] => G recipe
                )

        )

    [2] => Array
        (
            [0] => cat 2
            [1] => Array
                (
                    [0] => dal
                    [1] => fish
                )

        )

    [3] => Array
        (
            [0] => cat 3
            [1] => Array
                (
                )

        )

    [4] => Array
        (
            [0] => cat 4
            [1] => Array
                (
                )

        )

)

Thanks
Ripa Saha
于 2012-12-20T11:49:07.753 に答える
0

これで十分です...

     SET @rank = 0;
   SELECT cat_name,recipe_text  FROM (SELECT 
                  id,cat_id AS P,recipe_text,
                  CASE
                  WHEN @rowdata != pid 
                  THEN @rank := 0 & @rowdata := cat_id 
                  ELSE @rank := @rank + 1 
                  END AS rank 
                  FROM
                  recipes  
                  GROUP BY cat_id,
                   id 
                   ) X ,t
                   WHERE X.rank <= 3 AND X.p = t.cat_id;
于 2012-12-20T11:57:04.750 に答える