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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:34:51+00:00 2026-05-27T16:34:51+00:00

I am trying to combine these MySQL queries but not getting them right. Here

  • 0

I am trying to combine these MySQL queries but not getting them right. Here is the pseudocode that I am hoping to combine to get a single line sql statement.

$var1 = "abc"
$var2 = "def"

IF ( $var1 IN (SELECT DISTINCT col1 FROM t1) )
{
    SELECT colA, colB, colC FROM t1 WHERE col1 = $var1 AND col2 LIKE '%$var2%'
}
ELSE
{
    SELECT colA, colB, ColC FROM t1 WHERE col2 LIKE %$var1%
}

Thanks

  • 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-27T16:34:51+00:00Added an answer on May 27, 2026 at 4:34 pm

    First let me say that mjfgates could be right. The original psuedo code is not “bad” just because it takes two steps. The more complex your SQL statement that greater chance the query engine may not find an optimal plan. In this particular case that’s less likely because there’s just a single table we’re referencing multiple times, but it is something to keep in mind in general in these situations. Getting a SQL down to one statement is not always a worthy goal in itself.

    Now to answer your question:

       select colA,colB,colc from table1 t1
        where 
        (
        (col1 = $var1 and col2 like '%$var2%') and 
                EXISTS (select 1 from table1 t2 where t2.col1 = $var1)
        )
        or 
        (
        (col2 LIKE %$var1%) and 
               NOT EXISTS (select 1 from table1 t3 where t3.col1 = $var1)
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to save a step and combine these two queries but not sure how
I'm trying to combine these variables and append them to a select tag but
I'm trying to combine these two select queries but the count function is confusing
Can I combine these 2 SQL statements? Currently running 2 queries. Trying to tighten
I am trying to combine these 2 queries in such a way to determine
These are the two javascripts that i am trying to combine in the header
I am trying to combine these four get variables (sch, val, lmt, ord) into
I'm trying to combine multiple column values into 1 and then label them distinctly
I'm trying to combine 4 input fields into a jQuery function so that it
I'm trying to combine Spring with Hibernate using Annotations and I'm getting the following

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.