使用union内部查询返回查询结果

时间:2017-01-12 08:41:36

标签: php mysql codeigniter codeigniter-3

我在这个数据库中有3个表

availability

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)

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

如果同一close和同一room_idoffer_id中的任何行包含值1,我不想显示结果。

例如,您可以在表offer_day中看到room_id 8有两个日期&#39; 2017-01-24&#39;和&#39; 2017-01-25&#39;还有两个offer_id 12和14,即日期&#39; 2017-01-25&#39;在closedoffer_id中有1个。我不想显示room_id 8和offer_id 12,如果有任何行包含值1

我该怎么做?

谢谢。

2 个答案:

答案 0 :(得分:0)

您在评论中询问,如果关闭的记录为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.closed NOT IN (select closed 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.closed NOT IN (select closed 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.closed NOT IN (select closed FROM package_day WHERE closed = 1 AND date >= '2017-01-24' AND date <= '2017-01-26')

答案 1 :(得分:0)

如果您不想要房间,可以使用bleow查询

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中使用它。 现在你有你的ans?