【发布时间】:2012-07-20 06:36:42
【问题描述】:
我有这个架构:
Story
_id
name
Paragraph
_id
text
story_id
( the chapter can start from a specific story and
a specific paragraph and
ends with a specific story and specific paragraph).
Chapter
_id
name
start_story_id
start_paragraph_id( first paragraph id in the story which _id == start_story_id)
end_story_id
end_paragraph_id ( last paragraph id in the story which _id == end_story_id)
我想知道特定段落在哪一章中。我有故事 ID 和段落 ID。 这是我到目前为止所尝试的,它给出了正确的结果:
SELECT MIN(_id)
FROM chapter
WHERE ( start_story_id = @param_story_id
AND start_paragraph_id >= @param_parahgraph_id )
OR ( end_story_id = @param_story_id
AND end_paragraph_id >= @param_parahgraph_id )
OR ( @param_story_id >= start_story_id
AND @param_story_id <= end_story_id )
【问题讨论】:
-
你为什么不能只检查
(@paragraphid between start_paragraph_id and end_paragraph_id)的位置?每个新故事都会将段落 ID 重置回1吗? -
你为什么要在这里>=操作?
-
此外,章节和故事和段落之间是多对一的关系吗?或一对一(我从你的例子中假设)
-
一个章节可以跨越多个故事?!?如果 150 是段落 ID,为什么要将其与故事 ID(查询的最后一个子句)进行比较?
-
令人惊讶...还有为什么你需要
start_story_id,因为它可以从start_paragraph_id推导出来(end_story_id也一样)