简体   繁体   中英

SQL Server 2005 SELECT from multiple tables

How can I make this query to SELECT from 4 different tables and return the results ordered by date across all 4 tables ? (I need the latest 200 results ordered by date)

SELECT * 
FROM [CPU_Benchmarks] 
JOIN [CPU_Slugs] ON CPU_Benchmarks.Id = CPU_Slugs.BenchmarkId AND [Approved] = 'true' 
ORDER BY [TimeStamp] DESC

The tables are very similar

depending on what exactly you are trying to do, the UNION statement could help, for example:

SELECT TOP 200 col1, col2
FROM
(
    SELECT col1, col2 FROM table1
    UNION
    SELECT col1, col2 FROM table2
    UNION
    SELECT col1, col2 FROM table3
    UNION
    SELECT col1, col2 FROM table4
) myTableAlias
ORDER BY col1

you can of course enrich this with your joins or other required logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM