我有两个表Table1和Table2。表1中有10个字段,表2中有9个字段。两个表中都有一个共同的列,即AdateTime。此列保存用户操作的unix时间戳。我希望将两个表中的记录显示为单个结果,但必须根据AdateTime进行排序。应首先显示最近的操作。有时表1中有很多最近的动作,但表2中很少。反之亦然。所以我想使用单个查询从两个表中获取组合结果集。我正在使用PHP MySQL。
答案 0 :(得分:1)
尝试
SELECT t1.*, t2.*
FROM table1 t1 INNER JOIN table2 t2
ON t1.AdateTime = t2.AdateTime
ORDER BY t1.AdateTime
或(如果表格无关)
SELECT * FROM
(SELECT ADateTime, col1, col2, col3, col4 FROM table1
UNION
SELECT ADateTime, col1, col2, 1 AS col3, NULL AS col4 FROM table2) t2
ORDER by ADateTime
答案 1 :(得分:0)
我会将UNION ALL与内联视图一起使用。像
这样的东西select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime
from
(
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime from Table1
UNION ALL
select col1,col2,col3,col4,col5,col6,col7,col8,null as col9,AdateTime from Table2
) t
order by t.Adatetime desc;
答案 2 :(得分:0)
select t1.column_1234,t2.column_1234
from t1 table1 , t2 table2
where t1.matching_column = t2.matching_column
order by t1.AdateTime;
t1.matching_column And t2.matching_column are the Primary And Foreign keys for these tables (Matching Column)