从日期多表中获取数据

时间:2016-03-08 06:17:21

标签: mysql

假设:

表名:购买

id          pur_Date        pur_value 
1           01/02/16        5000.00
2           02/02/16        2500.00
3           05/02/16        7000.00

表名:销售

id          sale_Date       sale_value 
1           02/02/16        5050.00
2           02/02/16        2555.00
3           05/02/16        9000.00
4           05/02/16        7000.00
5           05/02/16        7250.00

运行查询...............

结果如下:

Date            Value
01/02/16        5000.00
02/02/16        2500.00
02/02/16        5050.00
02/02/16        2555.00
05/02/16        7000.00
05/02/16        9000.00
05/02/16        7000.00
05/02/16        7250.00

请解决。

3 个答案:

答案 0 :(得分:0)

使用UNION ALL

select t.* from
(
    select pur_Date as Date, pur_value as Value
    from purchase
    union all
    select sale_Date as Date, sale_value as Value
    from sale
)t
order by t.Date;

答案 1 :(得分:0)

试试这个。

(SELECT pur_Date, pur_value FROM purchase)
UNION ALL
(SELECT Date, Value FROM Sale)
ORDER BY 1;

答案 2 :(得分:0)

使用Union All查询并按键Date按其他假设字符串as排序:

SQL>

 select pur_Date as Date, pur_value as Value
 from purchase
            union all
 select sale_Date as Date, sale_value as Value
 from sale
            order by 1;