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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:19:14+00:00 2026-05-13T17:19:14+00:00

This is a theoretical question, I was wondering if there is a good way

  • 0

This is a theoretical question, I was wondering if there is a good way of finding out which condition in the WHERE statements matched.

Say I have a query like this:

SELECT * FROM table WHERE
    COND1 OR
    (COND2 AND COND3) OR
    COND4

Is there any way of knowing which of the conditions made a given row match (or unmatch)?


One solution i thought of was adding a CASE clause, to the SELECT, and rerunning all the WHERE conditions for that row:

SELECT *, which_cond = CASE .... END CASE ...
  • 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-13T17:19:15+00:00Added an answer on May 13, 2026 at 5:19 pm

    CASE is a possibility. But shorter to write would be IF() (at least with MySQL):

    SELECT *, IF(COND2, 1, 0) AS cond2, IF(COND3, 1, 0) as cond3 ... 
    

    and every cond* matched if its value is 1.

    Of course it does not make sense to check whether COND1 matches or not. For all results you get back, COND1 was true. So IF(COND1, 1, 0) will always return 1 (in your example).

    Update:
    Again at least for MySQL, I discovered, that the following is sufficient if you only want to get either 1 or 0 as result:

    SELECT *, COND2 AS cond2, COND3 as cond3 ...
    

    and with respect to Mark’s answer, you can avoid writing the conditions twice, if you use HAVING instead of WHERE (HAVING as access to aliases):

    SELECT *, COND2 AS cond2, COND3 as cond3
    FROM table
    HAVING COND1 AND (cond2 OR cond3)
    

    (note: uppercase means the actual condition expression and lowercase is the alias name)

    Update 2:

    Well it changes not that much: you only have to check conditions that are connected via OR in your updated version it would be sufficient to check whether COND2 is true or not. If so, then COND3 is also true and vice versa.

    SELECT *, COND1 AS cond1, COND2 as cond2, COND4 as cond4
    FROM table
    HAVING cond1 OR (cond2 AND COND3) OR cond4
    

    I think the point is clear.

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

Sidebar

Related Questions

I have been struggling to find an answer to this theoretical question, even tho
This is more of a theoretical question than an actual problem I have. If
This is basically a theoretical question on the way the decoding of a JSON
This is a theoretical question as I don't have an actual problem, but I
this is a theoretical question... Every example i have looked at uses the @SessionScoped
This is more of a theoretical question. If I have an arbitrary collection c
This is mainly a theoretical question i.e I don't have any need for a
This question is purely theoretical. I was wondering whether the Linux source code could
this is more theoretical question. I have written simple chat with my friend. We
This is a theoretical question; I don't have an actual issue to solve, 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.