Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:08:48+00:00 2026-06-18T05:08:48+00:00

given the following query SET @num :=0, @current_shop_id := NULL, @current_product_id := NULL; SELECT

  • 0

given the following query

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

I am expecting row_number values of the form

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

(ie, all unique product_id values associated with a given shop_id value get a unique row number, starting from 0). Instead, I am getting

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

what am I doing wrong?

EDIT:

Michael Berkowski’s solution (use three queries, one for retrieving data, one for adding row numbers, one for limiting by row) works (syntax slightly changed):

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;

Purely as a matter of educational interest, I’m wondering why it is not possible to add the row numbers and limit by row number at the same time, as in the following (non-functional) example

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:08:50+00:00Added an answer on June 18, 2026 at 5:08 am

    To get the rows to increment, it should be as simple a matter as moving the @num calculation into the outer query. And in that situation, the @current_product_id should not be needed as @num can increment directly.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given a query like the following: DELETE FROM FOO WHERE ID in (1,2,3,4,....); Is
Assume that the following query is issued to a MySQL database: SELECT * FROM
Given the following set of data, I'm trying to determine how I can select
I have the following query SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id FROM s s
I have the following query: $sqltext = SELECT Score FROM HighScore . WHERE fbID='$_POST[fbID]'
I was given a MySQL query: SET @COD_PAIS = 3, @ID_CARTERA = 8; SELECT
I have the following query: 'SELECT * FROM posts LEFT JOIN taxi ON taxi.taxiID
Given the following HQL Query: FROM Foo WHERE Id = :id AND Bar IN
Given the following query, how do I return the p_name with the most transactions?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.