gpt4 book ai didi

php - 返回带有 union inside 查询的查询结果

转载 作者:行者123 更新时间:2023-11-29 19:34:33 24 4
gpt4 key购买 nike

我的数据库中有 3 个这样的表

表格可用性

availability_id | date       | price | room_id | closed
-------------------------------------------------------
1 | 2017-01-24 | 75 | 8 | 0
2 | 2017-01-24 | 95 | 9 | 0
3 | 2017-01-25 | 80 | 73 | 0
4 | 2017-01-25 | 70 | 65 | 0
5 | 2017-01-26 | 85 | 42 | 0
6 | 2017-01-26 | 65 | 21 | 0

offer_day

plan_id | offer_id date | price      | room_id |    | closed 
------------------------------------------------------------
1 | 12 | 2017-01-24 | 70 | 8 | 0
2 | 23 | 2017-01-24 | 75 | 9 | 0
3 | 12 | 2017-01-25 | 70 | 8 | 1
3 | 14 | 2017-01-25 | 70 | 8 | 0
4 | 34 | 2017-01-25 | 75 | 9 | 0
5 | 43 | 2017-01-25 | 80 | 73 | 0
6 | 54 | 2017-01-25 | 85 | 65 | 0
7 | 65 | 2017-01-26 | 75 | 42 | 0
8 | 44 | 2017-01-26 | 70 | 21 | 0

package_day

package_id | date       | price | room_id | closed
--------------------------------------------------
1 | 2017-01-24 | 120 | 8 | 0
2 | 2017-01-24 | 125 | 9 | 0
3 | 2017-01-25 | 135 | 73 | 0
4 | 2017-01-25 | 130 | 65 | 0
5 | 2017-01-26 | 125 | 42 | 0
5 | 2017-01-26 | 120 | 21 | 0

我有这样的查询:

SELECT a.price 
FROM availability a
WHERE a.closed = 0 AND a.date >= '2017-01-24' AND a.date <= DATE_SUB('2017-01-26', INTERVAL 1 DAY)

UNION

SELECT 0.price
FROM offer_day o
WHERE o.closed = 0 AND o.date >= '2017-01-24' AND o.date <= DATE_SUB('2017-01-26', INTERVAL 1 DAY)

UNION

SELECT p.price
FROM package_day p
WHERE p.closed = 0 AND p.date >= '2017-01-24' AND p.date <= DATE_SUB('2017-01-26', INTERVAL 1 DAY)

如果我运行该查询,我将获得close = 0的所有行。正如您在表 offer_day 中看到的,close 列中的值为 1。

如果相同 room_id 和相同 offer_id 中的 close 列中的任何行包含值 1,我不想显示结果。

例如,您可以在表 offer_day 中看到 room_id 8 有两个日期“2017-01-24”和“2017-01-25”以及两个offer_id 12 和 14 在日期“2017-01-25”中,已关闭 中为 1,offer_id 8 中为 1。我不想显示 room_id 8 和 offer_id 12(如果任何行包含值 1)

我怎样才能做到这一点?

谢谢。

最佳答案

如果您不需要空间,可以使用以下查询

SELECT a.price 
FROM availability a
WHERE a.closed = 0 AND a.date >= '2017-01-24' AND a.date <= DATE_SUB('2017-01-26', INTERVAL 1 DAY) and a.room_id NOT IN (select room_id FROM availability WHERE closed = 1 AND date >= '2017-01-24' AND date <= '2017-01-26')

UNION

SELECT o.price
FROM offer_day o
WHERE o.closed = 0 AND o.date >= '2017-01-24' AND o.date <= DATE_SUB('2017-01-26', INTERVAL 1 DAY) and o.room_id NOT IN (select room_id FROM offer_day WHERE closed = 1 AND date >= '2017-01-24' AND date <= '2017-01-26')

UNION

SELECT p.price
FROM package_day p
WHERE p.closed = 0 AND p.date >= '2017-01-24' AND p.date <= DATE_SUB('2017-01-26', INTERVAL 1 DAY) and p.room_id NOT IN (select room_id FROM package_day WHERE closed = 1 AND date >= '2017-01-24' AND date <= '2017-01-26')

你可以看到如果关闭=1我得到它的房间ID并在房间ID中不使用它。现在你得到答案了吗?

关于php - 返回带有 union inside 查询的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41608526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com