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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:13:05+00:00 2026-06-09T16:13:05+00:00

I have a MySQL table for competitions called users . Now, I want to

  • 0

I have a MySQL table for competitions called users. Now, I want to be able to make a select statement that sorts this table by post_comp DESC, posted_last ASC. After this sort, whoever is returned first is #1, second row is #2 etc etc. However, I want to take this further and return the position of a specific user in the table. Now, each user has their own id, zbid. I also need to add WHERE cid=3 AND post_comp>0 to the end of the query.

This is what I have tried:

SELECT u.zbid,u.post_comp, @curRow := @curRow + 1 AS row, IF(u.zbid=123,1,0) AS me FROM users u, (SELECT @curRow := 0) r WHERE cid=3 AND post_comp > 0 ORDER BY u.post_comp DESC, u.posted_last ASC

Now what that does is return the following:
zbid, post_comp, row, me
where me is 1 if it is the row I specifically want to return, and 0 otherwise. So if I am looking for the position of the user with zbid=123, it sorts the table, brings all the data back to me, and wherever the user with zbid=123 is, will also return a me=1 result. row denotes the user’s position in the competition.

Now, I this works fine, only problem is that it returns all of these rows, and I only want it to return the one row with me=1. I tried appending another ORDER BY me DESC LIMIT 1 to the very end but that messed up the previous ordering and didn’t work. Any other suggestions?

  • 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-09T16:13:06+00:00Added an answer on June 9, 2026 at 4:13 pm
    SELECT * 
    FROM (
      SELECT u.zbid,u.post_comp, @curRow := @curRow + 1 AS row, 
             IF(u.zbid=123,1,0) AS me 
      FROM users u, (SELECT @curRow := 0) r 
      WHERE cid=3 AND post_comp > 0 
      ORDER BY u.post_comp DESC, u.posted_last ASC
    ) temp 
    WHERE me=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that looks something like this: +-----+------------+ | id |
I have a MySQL table called accounts . Within this table is a field
I have a MySQL table that looks something like this: |---ID---|---COUNTER---| | 1 |
I have MySQL table that doesn't have AUTO_INCREMENT field. In PHP I run this
I have a MySQL table called items that contains thousands of records. Each record
Let's assume we have MySQL table events from which we'll select a data. Now
I have mysql table that has a column that stores xml as a string.
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have a MySQL table with an auto-incremented integer primary key. I want to
I have a mysql table structure like that: id int primary key name varchar

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.