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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:34:19+00:00 2026-06-02T19:34:19+00:00

Basically I have this query: ( SELECT * FROM tbl WHERE type = ‘word1’

  • 0

Basically I have this query:

( SELECT * FROM tbl WHERE type = 'word1' )
  UNION
( SELECT * FROM tbl WHERE type = 'word2' ) // Run this query if there are no results with type = 1

Basically I would like to run the second query only if the first hasn’t any results. is it possible?

  • 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-02T19:34:20+00:00Added an answer on June 2, 2026 at 7:34 pm

    The FIRST “PreCheck” query does a count of how many records ARE of type = 1. After that, if the count is greater than 1, then return 1, otherwise return 2.

    Now, THAT answer can be used in the join (which is always a single row via COUNT(*)) which will either have a 1 or 2 value. THAT value will be the second value is the EQUALITY conditon. So, if there IS an entry of 1, the result will be as if

    WHERE t1.Type = 1

    Thus never allowing any 2 in the test. HOWEVER, if NO entries are found, it will have a value of 2 and thus create a WHERE clause of

    WHERE t1.type = 2

    select t1.* 
       from 
          ( select if( count(*) > 0, 1, 2 ) IncludeType
               from tbl t2
               where t2.type = 1 ) preCheck,
          tbl t1
       where 
          t1.type = preCheck.IncludeType
    

    If there is an index on the “type” column, the first query should be almost instantaneous.

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

Sidebar

Related Questions

Basically I have this string $str=word1 word2 word3; I need array( 'word1', 'word2', 'word3'
i have this query SELECT CASE WHEN EXISTS (SELECT PARM_VALUE FROM BO_PARM WHERE (ENTE_CD
I have this query SELECT articles.*, users.username AS `user` FROM `articles` LEFT JOIN `users`
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
I have this query that works: select name, location_id from all_names where location_id in
I have this query SELECT *, bp.product_id FROM builder_product as bp JOIN builder_step as
So i have this query: SELECT p.*, d.reviews, TRUNC(ds.ratingAvg, 2) as ratingAvg FROM place
so basically I have this scope(sql): scope.to_sql => SELECT \offers\.* FROM \offers\ INNER JOIN
I have this query: SELECT f.uid, fi.status FROM friends f LEFT JOIN friends_invitations fi
I currently have this query set-up: SELECT topic.content_id, topic.title, image.location FROM mps_contents AS topic

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.