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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:10:00+00:00 2026-06-07T17:10:00+00:00

I am struggle with mysql query. please help me. This is my query, i

  • 0

I am struggle with mysql query. please help me.
This is my query, i getting correct result but i need to modify the result in mysql.

                SELECT  bu.username,
                    bg.id as goal_id,
                    br.id as reason_id,
                    (SELECT COUNT(test_reason_id) FROM test_rank WHERE test_reason_id = br.id) as point
                FROM
                    test_goal AS bg INNER JOIN test_reason AS br ON 
                    br.user_id=bg.user_id INNER JOIN test_user AS bu ON
                    br.user_id=bu.id
                WHERE
                    bg.id       =   br.test_goal_id
                GROUP BY
                    bg.id
                ORDER BY
                    point DESC

Tabble-1
My actual table look like this when i use ORDER BY point DESC then its look like Table-2

username     goal_id    reason_id   point
 khan           8            3        2
 john           6            9        5
 yoyo           5           21        4
 smith         11            6        5

Tabble-2
My result set look like this

username     goal_id    reason_id   point
 john           6            9        5
 smith         11            6        5
 yoyo           5           21        4
 khan           8            3        2

But i want my result set like this

username     goal_id    reason_id   point   rank
 john           6            9        5       1
 smith         11            6        5       2
 yoyo           5           21        4       3
 khan           8            3        2       4

is this possible? please can any one help me. it too difficult for me.

  • 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-07T17:10:03+00:00Added an answer on June 7, 2026 at 5:10 pm

    Add a row count variable like this:

    select a.*,  (@row := @row + 1) as rank
    from (
          SELECT  bu.username,
                    bg.id as goal_id,
                    br.id as reason_id,
                    (SELECT COUNT(test_reason_id) FROM test_rank WHERE test_reason_id = br.id) as point                   
                FROM
                    test_goal AS bg INNER JOIN test_reason AS br ON 
                    br.user_id=bg.user_id INNER JOIN test_user AS bu ON
                    br.user_id=bu.id                    
                WHERE
                    bg.id       =   br.test_goal_id
                GROUP BY
                    bg.id
                ORDER BY
                    point DESC
    ) a, (SELECT @row := 0) r
    

    See this simplified SQLFiddle example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I struggle with dates and times in R, but I am hoping this is
I'm not totally new to SQL, but am rusty and struggle with MYSQL (using
I'm having a little struggle on this one and would appreciate some help. In
struggle extracting necessary data from this complex json stored inside php variable. Via php
I'm struggle extracting necessary data from this json stored inside php variable. Not yet
I struggle with JSON and jQuery so I'm hoping to get some help figuring
This is a continuation of the ongoing struggle to reduce my memory load mention
Last two nights I am struggle with below code. The problem is I need
Possible Duplicate: MySQL query using an array How to use an array of values
I'm building a blog page. But I'm starting to struggle on the archive section.

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.