【问题标题】:Select records with conditions in Join table or not existing in Join table选择 Join 表中有条件的记录或 Join 表中不存在的记录
【发布时间】:2018-01-21 10:31:30
【问题描述】:

我在 Postgres 9.5 数据库中有以下表:

rate_adjust

Column     | Type    | Modifiers
-----------+---------+----------------------------------------------
id         | integer | not null default nextval('product_days_id_seq'::regclass)
name       | text    |
amount     | integer |
product_id | integer | 
type       | integer |
 Indexes:
"pk_product_days" PRIMARY KEY, btree (id)

factor_calendar

 Column         |            Type             |                       Modifiers
----------------+-----------------------------+-----------------------------------------------------
id              | integer                     | not null default nextval('product_id_seq'::regclass)
from_day        | integer                     |
thru_day        | integer                     |
created_at      | timestamp without time zone | default now()
updated_at      | timestamp without time zone | default now()
rate_adjust_id  | integer                     |
 Indexes:
    "pk_product" PRIMARY KEY, btree (id)
  Foreign-key constraints:
    "fk_rate_adjust" FOREIGN KEY (rate_adjust_id) REFERENCES rate_adjust(id)

我需要获取rate_adjust 记录,这些记录要么在factor_calendar 表中没有foreign_key,要么如果它们的日期应该在from_daythru_day 范围内 我正在尝试使用以下查询,但也会返回不在该范围内的 rate_adjust 记录

   SELECT *
   FROM rate_adjust AS rad
   LEFT JOIN factor_calendar as fc ON (
       rad.id = fc.rate_adjust_id               
      WHERE  rad.product_id = p_id  
     AND start_day_id < end_day_id AND NOT (
       ( fc.from_day > start_day_id  AND fc.from_day > end_day_id)
       OR ( fc.thru_day < start_day_id AND fc.thru_day < end_day_id)
    )

factor_calendar中存储的数据是

 id | from_day | thru_day | rate_adjust_id     
 —--+----------+----------+----------------
  1 |       14 |       17 |              1
  2 |        1 |        3 |              2
  3 |        1 |        7 |              3

rate_adjust中存储的数据是

 id |          name          | type | amount | product_id
—--+------------------------+------+------+------------—
  1 | rateadjust 1           |    0 |   15 |            3
  2 | rateadjust 2           |    0 |   15 |            3
  3 | rateadjust 3           |    1 |   15 |            3
  4 | rateadjust 4           |    1 |   22 |            3

对于上述queryp_id = 3start_day_id = 4end_day_id = 15,我预计以下 rate_adjust 与 id(s) 124 被返回 但也返回了 id 3 的额定值

问题 如何修改我的查询以返回预期结果请考虑我也愿意接受任何建议以使用plpgsql 语言处理此问题

【问题讨论】:

  • 鉴于您的解释,我不明白为什么会返回 id 2 和 3。另外,解释什么“日期应该在 from_day 和 thru_day 范围内”——这是模棱两可的。是整天的意思吗?一天?完全巧合?

标签: sql postgresql join


【解决方案1】:

我相信你想要not exists——根据你的解释,而不是样本数据。

以下查询获取在 4-15 期间有一天的 rate_adjust 记录:

select ra.*
from rate_adjust ra
where not exists (select 1
                  from factor_calendar fc
                  where fc.rate_adjust_id = ra.id and
                        fc.from_date <= 15 and
                        fc.to_date >= 4
                 );

涵盖整个时期只是对逻辑的轻微修改。

此外,这不包括product_id。您的解释没有提到产品,根据提供的数据,这似乎是多余的。但是,添加相等条件是微不足道的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2019-09-02
    • 1970-01-01
    • 2020-12-18
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多