Daniel
Daniel

Reputation: 205

MySQL return all data from conditions

How can I return all data if the dataset is like this?

id    |     status    |     timestamp
1     |       1       |    2019-04-29
2     |       1       |    2019-04-28
3     |       2       |    2019-04-05
4     |       3       |    2019-04-04

I would like to get all entries from the dataset with a query like this:

WHERE status = 1 AND timestamp = '2019-04-29'
WHERE status = 2 AND timestamp = '2019-04-05'
WHERE status = 3 AND timestamp = '2019-04-04'

the data that it should return should be

id    |     status    |     timestamp
1     |       1       |    2019-04-29
3     |       2       |    2019-04-05
4     |       3       |    2019-04-04

Upvotes: 0

Views: 49

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

You can use or, but I think tuples make this simpler:

WHERE (status, timestamp) in ( (1, '2019-04-29'), (2, '2019-04-05'), (3, '2019-04-04'))

Upvotes: 4

Thorsten Kettner
Thorsten Kettner

Reputation: 94859

Here is the alternative with mere AND and OR and appropriate parentheses:

WHERE (status = 1 AND timestamp = DATE '2019-04-29')
   OR (status = 2 AND timestamp = DATE '2019-04-05')
   OR (status = 3 AND timestamp = DATE '2019-04-04')

But I, too, prefer tuples as shown by Gordon :-)

Upvotes: 0

Related Questions