简体   繁体   English

来自多个表的SQL Server 2005 SELECT

[英]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 ? 如何从4个不同的表中进行SELECT查询并返回所有4个表中按日期排序的结果? (I need the latest 200 results ordered by date) (我需要按日期排序的最新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 桌子很相似

depending on what exactly you are trying to do, the UNION statement could help, for example: 根据您要尝试执行的操作,UNION语句可能会有所帮助,例如:

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. 您当然可以通过联接或其他必需的逻辑来丰富它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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