隨着表table1
和table2
多個conditiions選擇需要選擇那些locationId
S和hour
S的table1
的行,其中tier
在table2
是high
在其中
表1
+------------+------+---------+----------+
| locationId | hour | metric1 | metric2 |
+------------+------+---------+----------+
| 1111 | 10 | 200 | 40 |
| 1111 | 9 | 300 | -20 |
| 1111 | 11 | 1800 | 300 |
| 1122 | 9 | 600 | 800 |
| 1122 | 11 | 2300 | -10 |
| 1133 | 8 | 10000 | 30 |
+------------+------+---------+----------+
表2
+------------+------+-------+---------+
| locationId | hour | value | tier |
+------------+------+-------+---------+
| 1111 | 10 | 1300 | high |
| 1111 | 9 | 900 | medium |
| 1111 | 11 | 200 | low |
| 1122 | 9 | 100 | low |
| 1122 | 11 | 2300 | high |
| 1133 | 8 | 1400 | high |
+------------+------+-------+---------+
如果它是一列 - 說locationId
我可以做類似
select * from table1
where locationId in (select locationId from table2 where tier='high');
我如何做到這一點的時候了locationId
,hour
對需要進行比較?
輸出
+------------+------+---------+----------+
| locationId | hour | metric1 | metric2 |
+------------+------+---------+----------+
| 1111 | 10 | 200 | 40 |
| 1122 | 11 | 2300 | -10 |
| 1133 | 8 | 10000 | 30 |
+------------+------+---------+----------+
一種選擇是有一個字段'locationId_hour'這是'加入到'table2'and使用''locationId' hour' colums級聯select * from table1 where locationId ||小時(從table2中選擇locationId_hour,其中tier ='high')' – user3206440