有人能指出我如何做这个SQL查询的正确方向吗?我有两个表coord_table和rm_table。我想执行一个查询,其中任何落在任何rm_table.start_loc或rm_table.end_loc之间的coord_table.loc将从查询结果中删除。
coord_table
coord_id loc
____________________
1 9
2 19
3 30
4 55
rm_table
rast_id start_loc end_loc
___________________________
1 10 20
2 50 60
query_result
coord_id loc
_____________
1 9
3 30
答案 0 :(得分:1)
编辑:这应该有效。它使用BETWEEN
语法:
SELECT a.* FROM coords_table a
LEFT JOIN
(
SELECT
*
FROM
(
SELECT
coords_id, loc, start_loc, end_loc,
(loc BETWEEN start_loc AND end_loc) as is_between
FROM
coords_table, rm_table
) a
WHERE a.is_between = 1
) b
ON a.coords_id = b.coords_id
WHERE b.coords_id IS NULL