Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: How to bulk SELECT rows with multiple pairs in WHERE clause

Let's say I have a table, email_phone_notes that looks like this:

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| email                 | varchar      | NO   | PRI | NULL    |       |
| phone                 | varchar      | NO   | PRI | NULL    |       |
| notes                 | text         | NO   |     | 0       |       |
+-----------------------+--------------+------+-----+---------+-------+

So, each email/phone combination is unique, but you could have several email addresses with different phone numbers and vice versa. This is a little contrived but it mirrors my scenario.

I'd like to do a query like this:

SELECT * FROM email_phone_notes  WHERE email = '[email protected]' AND phone = '555-1212';

But, I'd like to do multiple pairs at once so I don't have to make several SELECT queries. It's also important to keep the pairs together because I don't want to return an errant phone/email combination that wasn't requested.

I could do something like this, but for the possibility of several hundred values the query will be really long.

SELECT * FROM email_phone_notes WHERE ( 
  (email='[email protected]' && phone='555-1212') || 
  (email='[email protected]' && phone='888-1212') || 
   ...

Is there a more elegant solution, or should I stick with this?

like image 841
Newtang Avatar asked Nov 17 '12 00:11

Newtang


People also ask

How do I select multiple rows in MySQL?

To select last two rows, use ORDER BY DESC LIMIT 2.

How do I select multiple rows in a single column in MySQL?

MySQL | Group_CONCAT() Function. The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

Can you have multiple WHERE clauses in MySQL?

MySQL allows you to specify multiple WHERE clauses. These clauses may be used in two ways: as AND clauses or as OR clauses. What is Operator? An operator is a special keyword used to join or change clauses within a WHERE clause.


2 Answers

If you're after elegant SQL, you could use row constructors:

SELECT * FROM email_phone_notes WHERE (email, phone) IN (
  ('[email protected]'  , '555-1212'),
  ('[email protected]', '888-1212')
  -- etc.
);

However, that's not at all index-friendly and would not be recommended on a table of any significant size. Instead, you could materialise a table with your desired pairs and join that with your table:

SELECT * FROM email_phone_notes NATURAL JOIN (
  SELECT '[email protected]' AS email, '555-1212' AS phone
UNION ALL
  SELECT '[email protected]', '888-1212'
-- etc.
) t;

Or else pre-populate a (temporary) table:

CREATE TEMPORARY TABLE foo (PRIMARY KEY (email, phone)) Engine=MEMORY
  SELECT email, phone FROM email_phone_notes WHERE FALSE
;

INSERT INTO foo
  (email, phone)
VALUES
  ('[email protected]'  , '555-1212'),
  ('[email protected]', '888-1212')
  -- etc.
;

SELECT * FROM email_phone_notes NATURAL JOIN foo;
like image 89
eggyal Avatar answered Oct 05 '22 21:10

eggyal


You can use a row constructor like this:

SELECT *
FROM email_phone_notes
WHERE (email, phone) IN (
  ('[email protected]', '555-1212'),
  ('[email protected]', '888-1212')
)

SQLfiddle example

like image 30
AndreKR Avatar answered Oct 05 '22 21:10

AndreKR