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

  • Home
  • SEARCH
  • 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 741119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:35:43+00:00 2026-05-14T08:35:43+00:00

users table: id-user-other columns scores table: id-user_id-score-other columns They’re are more than one rows

  • 0

users table:

id-user-other columns

scores table:

id-user_id-score-other columns

They’re are more than one rows for each user, but there’s only two scores you can have. (0 or 1, == win or loss). So I want to output all the users ordered by the number of wins, and all the users ordered by the numbers of losses.

I know how to do this by looping through each user, but I was wondering how to do it with one query. Any help is appreciated!

  • 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-14T08:35:44+00:00Added an answer on May 14, 2026 at 8:35 am

    if you want two outputs you need two queries

    SELECT u.user,
    COUNT(s.score) as WINS
    FROM users u INNER JOIN scores s
         ON u.user_id = s.user_id 
    WHERE s.score = 1
    GROUP BY u.user_id (and any other columns you need from the user table)
    ORDER BY WINS
    

    You then do another query for the losses.
    Essentially this is the same as Adams response, but his response will give you a list of users ordered by wins, then ordered by losses.
    To get losses, just change the s.score=1 to s.score = 0

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

Sidebar

Related Questions

I am looking to iterate through a USERS table, and for each user selected
I have about 1 million data in users table. Now I want each user
I have the table users and scores . Here are the associations: belongs_to :user
I have a table users with primary index userId . Currently, each user has
Here's my simple scenario: I've a Users table and a Locations table. ONE User
I have a structure where the main table is USER, other tables include CATEGORY
I have a user table 'users' that has fields like: id first_name last_name ...
When registering my first user in table 'users' the id is the same value
I have a user system set up in a 'users' table, and I have
I have a table called Users ( class User < ActiveRecord::Base ) and a

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.