如何按日期以mysql顺序从多个表中获取数据?

时间:2015-10-18 11:22:29

标签: php mysql

如何在mysql中按日期从多个表中获取数据?

SELECT *
FROM
(
    select 0 as case_no,amount , remarks, str_to_date(date,'%d/%m/%Y') as dt from ig_referance_paid where  referance = '$_GET[id]'    
     UNION ALL
   select case_no ,amount , 0 as remarks, str_to_date(date,'%d/%m/%Y') as dt from ig_referance_income where  referance = '$_GET[id]' 
) T1
ORDER BY dt

我可以让任何更好的方法来变得简单。请帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

不需要子查询:

SELECT 0 AS case_no,
       amount,
       remarks,
       STR_TO_DATE(date,'%d/%m/%Y') AS dt 
FROM ig_referance_paid 
WHERE referance = '$_GET[id]'    
UNION ALL
SELECT case_no,
       amount,
       0 AS remarks,
       STR_TO_DATE(date,'%d/%m/%Y') AS dt 
FROM ig_referance_income
WHERE referance = '$_GET[id]'
ORDER BY dt;

SqlFiddleDemo