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 6659203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:00:20+00:00 2026-05-26T02:00:20+00:00

I have this query: SELECT @ROWNUM := CASE WHEN @PREV_CAT_ID=T1.category_id THEN @ROWNUM+1 ELSE 1

  • 0

I have this query:

SELECT @ROWNUM := CASE WHEN @PREV_CAT_ID=T1.category_id THEN @ROWNUM+1 ELSE 1 END AS RANK, 
  @PREV_CAT_ID:=T1.category_id, 
  T1.* 
  FROM(d.category_id
      FROM some_table d,
      (SELECT @ROWNUM := 0)R 
      ORDER BY d.category_id
  }T1 

I want this RANK column to increment for records with same category_id. When category_id increases, I want rank to reset to 1 and start incrementing again.

This works great when I execute my query from TOAD, but when inside my web application or when executed from phpMyAdmin the RANK is always 1, no matter what I do.

Does anyone have any idea, what the problem might be?

Thank You!

  • 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-05-26T02:00:21+00:00Added an answer on May 26, 2026 at 2:00 am

    You should reset all variables (@ROWNUM and @PREV_CAT_ID) every time. Try this query –

    SELECT
      category_id, rank
    FROM (
      SELECT 
        category_id
        , IF(@category_id = category_id, @num := @num + 1, @num := 1) AS rank
        , @category_id := category_id
      FROM some_table
      CROSS JOIN (SELECT @num := 0, @category_id:= NULL) reset_all_vars
      ORDER BY category_id
    ) t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: select qa_returns_items.item_code, (CASE status_code WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity)
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have this query: select BUnit, value from myTable where BUnit in (555,556,557,558) and
I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have this update query: UPDATE aggregate_usage_input t JOIN (SELECT t2.id FROM aggregate_usage_input t2
I have this query: SELECT @rownum:=@rownum+1 'no', m.title, m.author, REPLACE(SUBSTRING_INDEX(m.content, ' ', 20), '<br>',
I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN
I have this query: select distinct id,name from table1 For a given ID, the
I have this query: SELECT (SUM(tblTransaction.AmountPaid) - SUM(tblTransaction.AmountCharged)) AS TenantBalance, tblTransaction.TenantID FROM tblTransaction GROUP
I have this query: SELECT Items.Name, tblBooks.AuthorLastName, tblBooks.AuthorFirstName FROM Items WHERE Items.ProductCode IN (

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.