4

我在 MySQL 中有 2 个表,在这两个表中我都有 id、title、date 和其他一些字段,一个表的名称是news,另一个表是gallery.

    Table news :
    id   |  title   |   date       |  ...
   ------+----------+--------------+-------
    1    | TestN 1  |  2012-12-12  | ...
    2    | TestN 2  |  2012-12-14  | ...
    3    | TestN 3  |  2012-12-14  | ...



     Table gallery :
    id   |  title   |   date       |  ...
   ------+----------+--------------+-------
    1    | TestG 1  |  2012-12-10  | ...
    2    | TestG 2  |  2012-12-12  | ...
    3    | TestG 3  |  2012-12-15  | ...

我想知道如何从两个表中按日期顺序选择 id、title 记录?是否可以?

像这样:

   Result :
    id   |  title   |   date       |  ...
   ------+----------+--------------+-------
    1    | TestG 1  |  2012-12-10  | ...
    1    | TestN 1  |  2012-12-12  | ...
    2    | TestG 2  |  2012-12-12  | ...
    2    | TestN 2  |  2012-12-14  | ...
    3    | TestN 3  |  2012-12-14  | ...
    3    | TestG 3  |  2012-12-15  | ...
4

3 回答 3

4

采用UNION

SELECT id, title, date FROM news
UNION ALL
SELECT  id, title, date FROM gallery
ORDER BY date, id, title
于 2012-12-16T05:22:25.320 回答
0

为了实现这个使用 UNION 子句,像这样......

SELECT id,title,date FROM news 
UNION
SELECT id,title,date FROM gallery 
于 2012-12-16T05:22:13.647 回答
0

您可以使用以下查询

SELECT id,title,date FROM news 
UNION All
SELECT id,title,date FROM gallery 
ORDER BY date, id, title
于 2012-12-16T05:42:55.207 回答