如何在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
我可以让任何更好的方法来变得简单。请帮助解决这个问题。
答案 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
强>