为什么这个MySQL查询产生错误的行号?

时间:2013-02-02 17:32:29

标签: mysql

给出以下查询

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的解决方案(使用三个查询,一个用于检索数据,一个用于添加行号,一个用于按行限制)工作(语法略有改变):

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;

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的逻辑检查商店ID和产品ID。如果它们相同,则计数器递增,否则它重置为零。虽然您的结果集具有所有相同的商店ID,但它具有不同的商品ID。