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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:20:40+00:00 2026-06-04T12:20:40+00:00

I asked the question about multiple rows in a previous thread, but now I

  • 0

I asked the question about multiple rows in a previous thread, but now I need to know about a single row. This is an example query:

SELECT * FROM table1 WHERE this = 5 LIMIT 1

But if there is no rows found then I would it to then find the record with a different value:

SELECT * FROM table1 WHERE this = 1 LIMIT 1

So what I want is, find a row with this having the value of 5, if not found then default to finding a row with a value of 1. How do I do that in a single query? Would an OR statement work like so?

SELECT * FROM table1 WHERE this = 5 OR this = 1 LIMIT 1

Would the order of the OR statement try to find any row with the value of 5, if found then it will stop and return the row? IF not found then look for 1 and then return that row? If not then how do I do this with a single query where it searches for one value, if not found defaults to find another value?

I was thinking this query:

SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this ASC LIMIT 1

However this query would always return the value with 1 if both values were found right? So I tried this:

SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this DESC LIMIT 1

But this would always return the value of 5 if both rows were found correct?

  • 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-04T12:20:42+00:00Added an answer on June 4, 2026 at 12:20 pm

    Your question appears to prioritize the search for 5 and only if not found then search for 1. Hence your second solution fits the requirement fine. It will return the value of 5 irrespective of if 5 is only found or both 5 and 1 exist.

    the 2nd option

    SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this DESC LIMIT 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already asked a question about this yesterday, but now I have another :)
I asked a question about building custom Thread Safe Generic List now I am
I asked a question about Garbage Collection in Java in this topic . But
My apologies, I've actually asked this question multiple times, but never quite understood the
I asked this question about a year ago on another site but never got
In my previous question on this portal, I had asked about some insight about
I know, variations of this question had been asked before. But my case may
I know this question has been asked multiple times on this site in one
I asked a question about different testing frameworks yesterday. This question can be found
I previously asked a question about chaining conditions in Linq To Entities. Now I

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.