Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Where ... In ... AND where ... in ... should only match on same index

I have the query:

SELECT * FROM `users` 
WHERE (`firstname` LIKE 'Luke' AND `lastname` LIKE 'Skywalker') OR  
 (`firstname` LIKE 'Foo' AND `lastname` LIKE 'Bar') OR 
(`firstname` LIKE 'Tom' AND `lastname` LIKE 'Turner');

But i would like to make it a bit more readable by using a where ... in ... I tried

SELECT * FROM users 
WHERE `firstname` 
    IN ('Luke','Foo','Tom') AND `lastname` IN ('Skywalker','Bar','Turner');

But unfortunately this will also match "Tom Skywalker", "Foo Turner" and all mixes you can think off.

I have to select on first and lastname (perhaps more fields like DOB) since i am getting data from an external API and i have to check if those names are in our system.

like image 218
Werring Avatar asked Apr 11 '12 17:04

Werring


1 Answers

SELECT * 
FROM users
WHERE (firstname, lastname) 
      IN ( ('Luke', 'Skywalker') 
         , ('Foo' , 'Bar') 
         , ('Tom' , 'Turner')
         )
;
like image 183
ypercubeᵀᴹ Avatar answered Oct 05 '22 23:10

ypercubeᵀᴹ