Solved

mysql: how to formulate WHERE for  2-out-of-3   "matches"

Posted on 2013-01-14
7
203 Views
Last Modified: 2013-01-14
i need to select records where at least 2 of 3 conditions are met.
How is this best done?
Below is probe code for what i'm after...

SELECT * FROM tbl WHERE
    A = 1
    OR
    B = 2
    OR
    C = 3

if the WHERE clause were an "expression language" then something like the following might suffice  which "adds up"  the successful terms in the "equation"

WHERE  (
   (if (A=1) then 1 else 0)
+
   (if (B=2) then 1 else 0)
+
   (if (C=3) then 1 else 0)

   )>=2


thanks!
0
Comment
Question by:willsherwood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 333 total points
ID: 38775405
you could try

where       (case when A=1 then 1 else 0 end) +
      (case when B=2 then 1 else 0 end) +
      (case when C=3 then 1 else 0 end)  >= 2
0
 
LVL 48

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 167 total points
ID: 38775411
Not sure of the MySQL syntax, but try something like:

SELECT *,  (if(A=1, 1, 0) + if(B=2, 1, 0) + if(C=3, 1, 0)) as Matches
FROM yourTabletbl
WHERE (if(A=1, 1, 0) + if(B=2, 1, 0) + if(C=3, 1, 0)) >= 2
0
 
LVL 41

Accepted Solution

by:
ralmada earned 333 total points
ID: 38775416
mysql also supports if()

so

where       if(A=1, 1, 0) +
      if(B=2, 1, 0) +
      if(C=3, 1, 0)  >= 2
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Closing Comment

by:willsherwood
ID: 38775423
excellent, many thanks.
this is easier than i had feared.
appreciated
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 38775511
The advantage of including the Matches column in the query results is that you can then identify how many of the matches were made.

Another alternative, if you want to be able to identify which conditions matched by simply looking at the Matches field would be:

SELECT *,  (if(A=1, "A","") & if(B=2, "B", "") & if(C=3, "C", "")) as Matches
FROM yourTabletbl
WHERE LEN(if(A=1, "A","") & if(B=2, "B", "") & if(C=3, "C", "")) >= 2
0
 

Author Comment

by:willsherwood
ID: 38775581
clever,  THANK YOU!
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 38775679
glad to help
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question