简体   繁体   中英

Multiple Conditional Where Clause SQL

I have two tables, 1 of which has different values for different days, and the other determines which data I should be looking at from the first table. Here's an example:

mysql> select * from test_table;
+----+---------+---------------------+-------+
| id | test_id | ymd                 | value |
+----+---------+---------------------+-------+
|  1 |       1 | 2013-01-01 00:00:00 |     5 |
|  2 |       1 | 2013-01-02 00:00:00 |     5 |
|  3 |       1 | 2013-01-03 00:00:00 |     5 |
|  4 |       2 | 2013-01-01 00:00:00 |     5 |
|  5 |       2 | 2013-01-02 00:00:00 |     2 |
|  6 |       2 | 2013-01-03 00:00:00 |     3 |
|  7 |       2 | 2013-01-04 00:00:00 |     4 |
|  8 |       2 | 2013-01-05 00:00:00 |     5 |
|  9 |       3 | 2013-01-06 00:00:00 |     6 |
+----+---------+---------------------+-------+

and

mysql> select * from test_ymd;
+----+---------+---------------------+
| id | test_id | ymd                 |
+----+---------+---------------------+
|  1 |       1 | 2013-01-02 00:00:00 |
|  2 |       2 | 2013-01-03 00:00:00 |
+----+---------+---------------------+

I want to write a query like this:

mysql-local> select * from test_table where (test_id=1 and ymd>'2013-01-02') or (test_id=2 and ymd>'2013-01-03');
+----+---------+---------------------+-------+
| id | test_id | ymd                 | value |
+----+---------+---------------------+-------+
|  3 |       1 | 2013-01-03 00:00:00 |     5 |
|  7 |       2 | 2013-01-04 00:00:00 |     4 |
|  8 |       2 | 2013-01-05 00:00:00 |     5 |
+----+---------+---------------------+-------+

However, for a large # of test_ids, this obviously become gross. Is there a quick and easy way to do this in mysql?

UPDATE

A join is a good way to do this (Thanks Gordon)

mysql-local> select tt.* from test_table tt join test_ymd tymd on tt.test_id = tymd.test_id and tt.ymd > tymd.ymd;
+----+---------+---------------------+-------+
| id | test_id | ymd                 | value |
+----+---------+---------------------+-------+
|  3 |       1 | 2013-01-03 00:00:00 |     5 |
|  7 |       2 | 2013-01-04 00:00:00 |     4 |
|  8 |       2 | 2013-01-05 00:00:00 |     5 |
+----+---------+---------------------+-------+

I'm also curious though about whether or not there is a way to do it in the where clause.

You want a join:

select tt.*
from test_table tt join
     test_ymd tymd
     on tt.test_id = tymd.test_id and tt.ymd > tymd.ymd;

EDIT:

You can do this with an explicit join. A typical way would be to use exists :

select tt.*
from test_table tt
where exists (select 1
              from test_ymd tymd
              where tt.test_id = tymd.test_id and tt.ymd > tymd.ymd
             );

If you have an index on test_ymd(test_id, ymd) , then the exists has an advantage. If you have duplicate rows in the test_ymd table for one id, there is no danger of getting duplicates in the results.

Join two tables like

select temp.* from test_table temp join test_ymd temptymd
 on temp.test_id = temptymd.test_id and temp.ymd > temptymd.ymd;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM