2011-06-22 48 views
1
领域

我试图选择表NEWS和NEWS.timestamp和EVENT.datetime事件和秩序即将举行的活动和最新消息订购UNION使用从2个表

基本无序的查询如下:

SELECT event_id, title FROM events 
UNION 
SELECT news_id, subject FROM news 

回答

2

将每列中的列添加到您的选择列表中,并使用相同的列名别名。然后在底部只有ORDER BY

SELECT 
    event_id, 
    title, 
    datetime AS thedate 
FROM events 
UNION 
SELECT 
    news_id, 
    subject, 
    timestamp AS thedate 
FROM news 
ORDER BY thedate 
1

在迈克尔的回答微小变化(如果你想通过时间戳和日期时间订购,但没有表现出他们):

SELECT 
    event_id, 
    title 
    FROM 
    (SELECT 
     event_id, 
     title, 
     datetime AS thedate 
     FROM events 
    UNION ALL 
     SELECT 
     news_id, 
     subject, 
     timestamp AS thedate 
     FROM news 
    ) AS tmp 
    ORDER BY thedate