【发布时间】:2018-05-16 03:43:27
【问题描述】:
我有两个表,表一个有两个范围,我用来查询第二个表,我想在一个查询中执行此操作到目前为止,我有两个单独的查询,但似乎无法找出如何一起使用它们.
This is table one, with the ranges that are from_book_id, from_chapter, to_book_id and to_chapter.
这是我用来获取所需行的查询:
SELECT * FROM plans WHERE plans.day = 1 AND plans.plan_id = 1
This is the second table with the data to filter with tables one range.
我用它从这个表中得到想要的结果
SELECT verse.* FROM verse WHERE (verse.book_id >= 1 AND verse.book_id <= 1) AND (verse.chapter >= 1 AND verse.chapter <= 5))
为了澄清,我需要使用第一个表中的结果范围作为第二个表的查询:
SELECT t1.* FROM t1 WHERE (t1.book_id >= t2.from_book_id AND t1.book_id <= t2.to_book_id) AND (t1.chapter >= t2.from_chapter AND t1.chapter <= t2.to_chapter))
我希望这个解释足够清楚,我提前谢谢大家。
编辑
我似乎在查询中遇到了一些特殊情况,尽管当我遇到以下范围时它工作得非常好:
from_book_id:1, from_chapter:1, to_book_id:1, to_chapter:5
该范围返回所需的结果,但是当范围从一本书更改为另一本书时,如下所示:
from_book_id:1, from_chapter:5, to_book_id:2, to_chapter:5
这意味着它需要获取从第 1 章第 5 章到第 1 章最后一章的所有内容,然后继续第 2 章直到第 5 章。
这些情况会破坏查询并返回 null,如果您能帮我解决这个问题,我将不胜感激。
再次感谢!
【问题讨论】:
标签: mysql sql sql-server sqlite