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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:06:34+00:00 2026-05-15T17:06:34+00:00

if select * from table where x=1 returns 0 rows, then I need select

  • 0

if select * from table where x=1 returns 0 rows, then I need select * from table where x=2 [or some other query]. Is it possible to do this in a single MySQL query with a conditional statement?

Edit: All answers with UNION work, but only if both queries select from the same table (or tables with the same number of columns). What if the second query is applied on a different table with joins?

Let me write down the my queries to make the question more clear:

1st:

SELECT  table1.a, table2.b  from table1 LEFT JOIN table2 ON table2.x= table1.x
WHERE ..... 

if the result from the 1st one is null then:

2nd:

SELECT table1.a FROM table1 
WHERE ....

I will be using the rows from the 1st query if it returns any, otherwise the 2nd one will be used.

  • 1 1 Answer
  • 1 View
  • 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-15T17:06:35+00:00Added an answer on May 15, 2026 at 5:06 pm

    This appears to work from a quick test I just did and avoids the need to check for the existence of x=1 twice.

    SELECT SQL_CALC_FOUND_ROWS *
    FROM mytable
    WHERE x = 1
    
    UNION ALL
    
    SELECT *
    FROM mytable
    WHERE 
    FOUND_ROWS() = 0 AND x = 2;
    

    Edit: Following your clarification to the question obviously the 2 queries will need to be UNION compatible for the above to work.

    The answer to your updated question is No. This is not possible in a single query. You would need to use some conditional procedural logic to execute the desired query.

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

Sidebar

Related Questions

i have this sql query select * from table where name like ? but
I like to reverse this query: SELECT * FROM table WHERE status IN (
I need to query the MYSQL with some condition, and get five random different
Ok, so basically I need to select multiple IDs from one column, then use
Having looked at some of the other mysql availability query questions on here has
I have this postgres sql query: select * from stats where athlete_id = 5
I need some help creating a query for my mySQL database. I have recently
SELECT * FROM [Table] WHERE (Izdava NOT LIKE NULL) // how to check if
When I perform SELECT * FROM table I got results like below: 1 item1
how to do a select * from table in jython and get the result

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.