SQL Server 2005从多个表中选择

时间:2021-05-08 23:50:48

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)

如何使此查询从4个不同的表中选择并返回所有4个表中按日期排序的结果?(我需要按日期最新订购的200份结果)

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

这些表非常相似。

1 个解决方案

#1


4  

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.

当然,您可以使用连接或其他必需的逻辑来丰富它。

#1


4  

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.

当然,您可以使用连接或其他必需的逻辑来丰富它。