1

次のクエリが与えられた

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

SELECT * FROM (

SELECT products.shop_id, products.product_id, @num := IF(@current_shop_id=shops.shop_id, IF(@current_product_id=products.product_id,@num,@num+1),0) AS row_number, @current_shop_id := shops.shop_id AS shop_dummy, @current_product_id := products.product_id AS product_dummy

    FROM
    favorites fav1 INNER JOIN
    products ON
    fav1.product_id=products.product_id AND 
    fav1.current=1 AND
    fav1.closeted=1 AND 
    fav1.user_id=30  INNER JOIN

    shops ON
    shops.shop_id = products.shop_id

    ORDER BY shops.shop ASC, products.product_id DESC

) AS rowed_results WHERE rowed_results.row_number>=0 AND rowed_results.row_number<(20) AND shop_id=130

フォームのrow_number値を期待しています

+---------+------------+------------+------------+---------------+
| shop_id | product_id | row_number | shop_dummy | product_dummy |
+---------+------------+------------+------------+---------------+
|     130 |    1153746 |          0 |        130 |       1153746 |
|     130 |    1153736 |          1 |        130 |       1153736 |
|     130 |    1139944 |          2 |        130 |       1139944 |
|     130 |    1098296 |          3 |        130 |       1098296 |
|     130 |    1017455 |          4 |        130 |       1017455 |
|     130 |     551953 |          5 |        130 |        551953 |
|     130 |     551914 |          6 |        130 |        551914 |
+---------+------------+------------+------------+---------------+

(つまり、特定のshop_id値に関連付けられたすべての一意のproduct_id値は、0から始まる一意の行番号を取得します)。代わりに、私は

+---------+------------+------------+------------+---------------+
| shop_id | product_id | row_number | shop_dummy | product_dummy |
+---------+------------+------------+------------+---------------+
|     130 |    1153746 |          1 |        130 |       1153746 |
|     130 |    1153736 |          0 |        130 |       1153736 |
|     130 |    1139944 |          0 |        130 |       1139944 |
|     130 |    1098296 |          0 |        130 |       1098296 |
|     130 |    1017455 |          0 |        130 |       1017455 |
|     130 |     551953 |          1 |        130 |        551953 |
|     130 |     551914 |          0 |        130 |        551914 |
+---------+------------+------------+------------+---------------+

私は何が間違っているのですか?

編集:

Michael Berkowskiのソリューション(3つのクエリを使用します。1つはデータの取得、1つは行番号の追加、もう1つは行による制限)が機能します(構文がわずかに変更されました)。

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

#limit by row

SELECT * FROM (

    #add row

    SELECT limit_query.*, @num := IF(@current_shop_id=shop_id, IF(@current_product_id=product_id,@num,@num+1),0) AS row_number, @current_shop_id := shop_id AS shop_dummy, @current_product_id := product_id AS product_dummy FROM (

        #retrieve data

        SELECT row_query.* FROM (

            SELECT products.shop_id, products.product_id

            FROM
            favorites fav1 INNER JOIN
            products ON
            fav1.product_id=products.product_id AND 
            fav1.current=1 AND
            fav1.closeted=1 AND 
            fav1.user_id=30  INNER JOIN

            shops ON
            shops.shop_id = products.shop_id

        ) AS row_query ORDER BY shop_id ASC, product_id DESC

    ) AS limit_query

) AS rowed_results WHERE rowed_results.row_number>=0 AND rowed_results.row_number<(20) AND shop_id=130;

純粋に教育上の関心事として、次の(機能しない)例のように、行番号を追加すると同時に行番号で制限することができないのはなぜだろうかと思います。

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

#limit by row

SELECT rowed_results.*, @num := IF(@current_shop_id=shop_id, IF(@current_product_id=product_id,@num,@num+1),0) AS row_number, @current_shop_id := shop_id AS shop_dummy, @current_product_id := product_id AS product_dummy FROM (

    #retrieve data

    SELECT row_query.* FROM (

        SELECT products.shop_id, products.product_id

        FROM
        favorites fav1 INNER JOIN
        products ON
        fav1.product_id=products.product_id AND 
        fav1.current=1 AND
        fav1.closeted=1 AND 
        fav1.user_id=30  INNER JOIN

        shops ON
        shops.shop_id = products.shop_id

    ) AS row_query ORDER BY shop_id ASC, product_id DESC

) AS rowed_results WHERE row_number>=0 AND row_number<(20) AND shop_id=130;
4

2 に答える 2

1

@num行をインクリメントするには、計算を外部クエリに移動するのと同じくらい簡単なことです。そして、そのような状況では、直接インクリメントできるので、は@current_product_id必要ありません。@num

SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL;

SELECT 
  *,
  /* Perform the row increment in the outer query so it acts on the final rowset */
  @num := @num+1 AS row_number
FROM (
    SELECT products.shop_id, products.product_id, @current_shop_id := shops.shop_id AS shop_dummy, @current_product_id := products.product_id AS product_dummy

    FROM
    favorites fav1 INNER JOIN
    products ON
    fav1.product_id=products.product_id AND 
    fav1.current=1 AND
    fav1.closeted=1 AND 
    fav1.user_id=30  INNER JOIN

    shops ON
    shops.shop_id = products.shop_id

    ORDER BY shops.shop ASC, products.product_id DESC

) AS rowed_results 
WHERE
  rowed_results.row_number>=0
  AND rowed_results.row_number<(20)
  AND shop_id=130
于 2013-02-02T17:37:09.873 に答える
0

ロジックはショップIDと製品IDの両方をチェックします。それらが同じである場合、カウンターは増加し、そうでない場合はゼロにリセットされます。結果セットにはすべて同じショップIDがありますが、製品IDは異なります。

于 2013-02-02T17:41:55.893 に答える