我有两个表,这些是下面的示例。
我想将它们加入到一个mysql查询中,按日期排序( tbl1_date 和 tbl2_date )。我想要的结果就像这张照片。
查询怎么样?谢谢。
答案 0 :(得分:0)
试试这个:
降序:
select * FROM table_1
UNION
select * FROM table_2
ORDER BY tbl1_date DESC;
升序:
select * FROM table_1
UNION
select * FROM table_2
ORDER BY tbl1_date ASC;
示例数据:
CREATE TABLE table_1
(
tbl1_id int auto_increment primary key,
mat_id int,
tbl1_desc varchar(30),
tbl1_date date
);
INSERT INTO table_1
(mat_id, tbl1_desc, tbl1_date)
VALUES
(2,'Lorem','2014-01-02'),
(4, 'Ipsum','2014-01-10');
CREATE TABLE table_2
(
tbl2_id int auto_increment primary key,
mat_id int,
tbl2_desc varchar(30),
tbl2_date date
);
INSERT INTO table_2
(mat_id, tbl2_desc, tbl2_date)
VALUES
(2,'Sit','2014-01-16'),
(4, 'Amet','2014-01-12');