简体   繁体   English

使用BETWEEN时进行正确的INNER JOIN

[英]Make the correct INNER JOIN when using a BETWEEN

I'm sorry if this is a dumb question, but I have this particular case I can't figure how to handle. 很抱歉,如果这是一个愚蠢的问题,但是我有这种特殊情况,我不知道如何处理。 I need a query where I get all date values between two date values on another tables, and right now this is my query 我需要一个查询,让我得到另一个表上两个日期值之间的所有日期值,现在这是我的查询

SELECT      h.hour_gkey, h.hour_time
FROM        Hours as h
INNER JOIN  ServiceHours sh ON h.hour_gkey BETWEEN sh.openhour_hour_gkey AND sh.closehour_hour_gkey;

So to explain it a bit further, the ServiceHours table has two fields openhour_hour_gkey and closehour_hourg_key that are integer, this two fields contain Foreign Keys of the Hours table and therefore they have time values, the hour_gkey(integer) its the primary key of Hours table and I need to show only the values of hour_time (date fieldtype) that are between the dates that correspond to those two fields. 因此,为了进一步解释,ServiceHours表具有两个为整数的字段openhour_hour_gkey和closehour_hourg_key,这两个字段包含Hours表的外键,因此它们具有时间值,hour_gkey(integer)是Hours表的主键并且我只需要显示在对应于这两个字段的日期之间的hour_time(日期字段类型)的值。 How could I do that 我该怎么做

Using right now SQL Server 2014 立即使用SQL Server 2014

Here is one way I think it could be done. 我认为这是可以实现的一种方法。 I am not certain if the syntax is exactly right for SQL Server. 我不确定语法是否完全适合SQL Server。 But the basic idea is, you would need to join from ServiceHours to Hours to get the actual open/close hour values, then select the rows from Hours with values in that range. 但是基本思想是,您需要从ServiceHours到Hours进行连接,以获取实际的打开/关闭小时值,然后从Hours中选择值在该范围内的行。

WITH min_max AS (
  SELECT h1.hour_time min_hour_time, h2.hour_time max_hour_time
  FROM ServiceHours sh
  JOIN Hours h1 ON h1.hour_gkey = sh.openhour_hour_gkey
  JOIN Hours h2 ON h2.hour_gkey = sh.closehour_hour_gkey
)
SELECT h.hour_time
  FROM Hours h
  JOIN min_max ON h.hour_time BETWEEN min_max.min_hour_time AND min_max.max_hour_time

(Note I'm assuming that ServiceHours has only one row. If it doesn't, there is probably some other field you want to include in both the subquery and the main query to indicate which row in ServiceHours each resulting row relates to.) (请注意,我假设ServiceHours只有一行。如果没有,则可能要在子查询和主查询中都包含其他字段,以指示ServiceHours中的每一行都与哪一行相关。)

I'm interpreting your question to be "How do I select all of the rows of Hours whose hour_time values are between those related to ServiceHours.openhour_hour_gkey and ServiceHours.closehour_hour_gkey ?" 我将您的问题解释为“我如何选择Hours hour_time值在与ServiceHours.openhour_hour_gkeyServiceHours.closehour_hour_gkey相关的值之间的所有Hours行?”

I furthermore suppose that it is intentional that you are neither selecting any columns from ServiceHours nor filtering to narrow the results to those associated with a single ServiceHours row. 我进一步假设,您既无意从ServiceHours选择任何列,也没有进行过滤以将结果范围缩小到与单个ServiceHours行相关的结果。 Thus, if there are multiple ServiceHour rows you will get a set of Hours rows for each one, with these sets not necessarily being disjoint, and with no indication of which goes with which ServiceHour . 因此,如果有多个ServiceHour行,您将为每个行获得一组Hours行,这些行不一定是不相交的,并且没有指示哪个ServiceHour与之对应。

In any case, you need to perform a join for each relationship you want to traverse, and for this query you seem to want another join to get the target data. 无论如何,您都需要为要遍历的每个关系执行联接,并且对于此查询,您似乎希望另一个联接来获取目标数据。 that might look like this: 可能看起来像这样:

SELECT h.hour_gkey, h.hour_time
FROM        
  Hours h
  CROSS JOIN  ServiceHours sh
  INNER JOIN  Hours sho
    ON sh.openhour_hour_gkey = sho.hour_gkey
  INNER JOIN  Hours shc
    ON sh.closehour_hour_gkey = shc.hour_gkey
WHERE h.hour_time BETWEEN sho.hour_time AND shc.hour_time;

I have written the BETWEEN condition as a filter predicate instead of a join predicate because that seems a better characterization. 我将BETWEEN条件写为过滤谓词而不是连接谓词,因为这似乎是更好的表征。 For inner joins, however, the two alternatives are equivalent. 但是,对于内部联接,这两种选择是等效的。 Note also that this query is semantically equivalent to @DaveCosta's. 还要注意,此查询在语义上等效于@DaveCosta。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM