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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:38:44+00:00 2026-06-07T23:38:44+00:00

I have three columns in a table: score status No. 1, 2, 1 0,

  • 0

I have three columns in a table:

score  status  No.
1,     2,      1
0,     1,      2
0,     0,      1

I need this, to write a C# style pseudo SQL:

rows = empty;

rows = "SELECT * FROM `table` WHERE score = 1"
if (rows.Count > 0) //at least one row
    return rows;

rows = "SELECT * FROM `table` WHERE status = 2"
if (rows.Count > 1) //more than one row 
    return row with MAX(No.) from rows; //ie MAX(No.) where status = 2

 return rows;

I hope I could be clear. In short, select from my table records with score = 1, and if there isn’t such a record, return the record where status = 2 and if there are more than one record with status = 2, then return the record with maximum value for No. where status = 2 (if no record at all with status = 2, return empty).

How can I write it in one query? It should be a good learning experience for me. Otherwise I know to breakup into smaller queries and run each one. And I can’t go with stored procedures right now..

Edit: Actually my query will have a few more WHERE clauses but identical in both the conditions and that is why I omitted it. Hence, regarding the first condition, there will be only one record returned for now. That is SELECT * FROM table WHERE score = 1 will return just one row for now. And I need / I’ll accept answers that gives such a solution too. But the point is you never know, may be in future with some design changes, there could be more rows with score = 1. That is why I went for records instead of record. But ideally the business logic is to have all records with score = 1. For now, record will do. I’m just thinking query will be much simpler if only one row is returned and my teammates can assimilate the code easily.

Final Update: Thank you all guys, you’ve been very kind 🙂 Many answers worked well, and choosing one is really really daunting. My finding on the answers:

  1. Answers which worked always: @GordonLinoff’s, @ZaneBiens’s, @ZaneBien’s another, @Scen’s, @JulienCh.’s (the last 3 being essentially the same, but yet I am not fully aware how those worked :))

  2. Answers which worked only when first condition score = 1 returned only one row: @HannoBinder’s, @ShlomiNoach’s, @DaniellePaquette-Harvey’s. For the moment, I ‘ll stick with @Danielle’s (which is freakingly simple) and later revert if there arises need to have more than one row)

  3. Rest of the answers, I couldn’t test as they were either not very specific or not related to MySQL.

  4. @MatthewPK’s is not appropriate in the context.

Awarding the bounty and accepting an answer is tough with so many right answers. I chose this one since I felt it is probably more efficient and readable too, but I’ll accept @Scen’s answer for being downright simple.

  • 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-07T23:38:47+00:00Added an answer on June 7, 2026 at 11:38 pm
    Select *
    From scores
    Where score = 1
    UNION
    (
       Select *
       From scores
       Where status = 2 and not exists (Select * from scores where score = 1)
       Order By `No.` desc
       Limit 1
    );
    

    I named table as scores.

    SqlFiddle (Query with some fake schema so you can see the results):
    http://sqlfiddle.com/#!2/6aac2/1/0

    EDIT: edited SQlFiddle to match the fake schema with the one in the question.

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

Sidebar

Related Questions

I have three columns in mysql table: tax_id, company_name, store_name. I need that tax_id
I have a table with three columns, and two cells contain select boxes with
I have a mysql table with three columns: username, location, timestamp. This is basically
I have a SQL-table with three columns: Id, English and Norwegian. Id is the
Consider i have a user table and I have three columns mobilePhone , homePhone
I have three tables, Name - columns Custpackingslipjour(table 1) - (deliverydate, dateclosed,, salesid) Inventrans
I have a table in HTML which has three columns i.e Error - User
I have a table of users with three columns. Username Accepted Rejected User 1
I have a (simplified) table consisting of three columns: id INT PRIMARY KEY NOT
I have three mysql table from same database Db1. Three tables have following columns.

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.