完全加入两个查询

时间:2017-02-15 15:14:00

标签: mysql sql

此查询返回385个字符串

SELECT order_number
FROM `order` 
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')

这返回了74个字符串

SELECT order_number
FROM `order` 
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN  DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY) 
AND  DATE_SUB(CURRENT_DATE, INTERVAL 7  DAY)
and order_delivery_data_name in ('London')

如何加入它,那就是有两列

2 个答案:

答案 0 :(得分:1)

 try this:
 SELECT t1.order_number as order_t1, t2.order_number as order_t2 FROM
        (       SELECT order_number,`order`.order_id 
                FROM `order` 
                inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
                where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN  DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY) 
                AND  DATE_SUB(CURRENT_DATE, INTERVAL 7  DAY)
                and order_delivery_data_name in ('London')

        ) as t1
        LEFT JOIN
        (SELECT order_number,`order`.order_id 
                FROM `order` 
                inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
                where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN  DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY) 
                AND  DATE_SUB(CURRENT_DATE, INTERVAL 7  DAY)
                and order_delivery_data_name in ('London')
        ) as t2
        ON t1.order_id  = t2.order_id
    UNION ALL
        SELECT t1.order_number as order_t1, t2.order_number as order_t2 FROM
        (       SELECT order_number,`order`.order_id 
                FROM `order` 
                inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
                where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN  DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY) 
                AND  DATE_SUB(CURRENT_DATE, INTERVAL 7  DAY)
                and order_delivery_data_name in ('London')

        ) as t1
        RIGHT JOIN
        (SELECT order_number,`order`.order_id 
                FROM `order` 
                inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
                where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN  DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY) 
                AND  DATE_SUB(CURRENT_DATE, INTERVAL 7  DAY)
                and order_delivery_data_name in ('London')
        ) as t2
        ON t1.order_id  = t2.order_id;

答案 1 :(得分:0)

使用mysql时需要使用FULL JOIN,即UNION ALL:

first_query UNION ALL second_query