2011-06-22 50 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