Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Warum erzeugt diese MySQL-Abfrage die falschen Zeilennummern?

Um die Zeilen zu inkrementieren, sollte es so einfach sein wie das Verschieben von @num Berechnung in die äußere Abfrage. Und in dieser Situation die @current_product_id sollte nicht als @num benötigt werden kann direkt inkrementieren.

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