我想連接兩個具有相同結構的表,如果它們匹配第三個表的主鍵。然後我想根據第三個表中的列進行排序。如果主鍵與第三個表匹配並按第三個表排序,我將如何連接2個表?
結構示例:
Table 1 - id (VARCHAR) | title (VARCHAR) | expire (TIMESTAMP)
Table 2 - id (VARCHAR) | title (VARCHAR) | expire (TIMESTAMP)
Table 3 - id (VARCHAR) | views (VARCHAR) | expire (TIMESTAMP)
我想這應該工作,但它並不:
SELECT * FROM table1 as t, table3 as t3 WHERE t.id = t3.id
UNION
SELECT * FROM table2 as t2, table3 as t3 WHERE t2.id = t3.id
ORDER BY table3.expire DESC
什麼能做到這一點?
試試這個: http://stackoverflow.com/questions/213851/sql-query-using-order-by-in-union – John 2012-03-15 01:20:35