【发布时间】:2018-05-17 12:37:15
【问题描述】:
我有两个配置单元表 table1 和 table2。两个表中没有相同的列。
表1:
------
| id |
------
| 22 |
| 11 |
| 56 |
| 15 |
------
表2:
-------------
| from | to |
-------------
| 10 | 20 |
| 21 | 35 |
| 40 | 60 |
-------------
table1的id列必须从table2的'from'和'to'列中检查它属于哪个范围。
预期输出:
------------------
| id | from | to |
------------------
| 22 | 21 | 35 |
| 11 | 10 | 20 |
| 56 | 40 | 60 |
| 15 | 10 | 20 |
------------------
尝试使用交叉连接和where 条件并能够获得所需的输出(但希望避免交叉连接)。还尝试使用“存在”命令但在获取输出时遇到错误:
查询:
select id from table1 t1
where exists(select 1 from table2 t2
where t1.id between t2.from and t2.to) ;
但出现错误:subquery expression refers to both parent and subquery expressions and is not a valid join condition。
任何建议都会有帮助。
谢谢
【问题讨论】: