从多个 SQL Server 表中选择 TOP 4 记录.使用 vb.net

Selecting TOP 4 records from multiple SQL Server tables. Using vb.net(从多个 SQL Server 表中选择 TOP 4 记录.使用 vb.net)
本文介绍了从多个 SQL Server 表中选择 TOP 4 记录.使用 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 4 个具有完全相同列名的不同表.我想要做的是从所有这些按日期排序的表中选择前 4 条记录(因为日期是它们共享的列之一).

I have about 4 different tables with the exact same column names. What I would like to do is select the top 4 records out of all of these tables combined ordered by date (as date is one of the columns that they all share).

我不断收到错误的陈述,无论是语法问题还是含糊不清的记录等.

I keep getting erroneous statements, whether it be a syntax issue or ambiguous record etc.

基本上我的陈述类似于:

Essentially my statement is similar to:

SELECT TOP 4 date, link, anchor, thumb FROM archive1, archive2, archive3, archive4 ORDER BY date DESC

显而易见的是,我对所有这些东西都不熟悉.在此先感谢您的帮助.

To state the obvious, I'm new to all this stuff. Thanks in advance for any assistance.

推荐答案

您需要制作 union 所有表,然后对它们进行排序以获得最后四个:

You need to produce union of all tables and then order them to get last four:

SELECT TOP 4 date, link, anchor, thumb 
FROM
(
  SELECT date, link, anchor, thumb 
    FROM archive1
   UNION ALL
  SELECT date, link, anchor, thumb 
    FROM archive2
   UNION ALL
  SELECT date, link, anchor, thumb 
    FROM archive3
   UNION ALL
  SELECT date, link, anchor, thumb 
   FROM archive4 
) archive
ORDER BY date DESC

由于您只获取 4 条记录,您可以向每个查询添加 TOP 4 子句以及 ORDER BY 日期 DESC 以加快速度.

As you only take four records you might add TOP 4 clause to each query along with ORDER BY date DESC to speed things up.

这篇关于从多个 SQL Server 表中选择 TOP 4 记录.使用 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)