简体   繁体   English

如何从SQL Server中的两个不同表中选择相同数量的记录

[英]How can I select the same number of records from two different tables in SQL Server

How can I select the same number of records from two different tables in SQL Server? 如何从SQL Server的两个不同表中选择相同数量的记录?

For example, I have a table A with 10 records and table B with 15 records; 例如,我有一个包含10条记录的表A和具有15条记录的表B; how can I select 10 records from A and 10 records from B ? 如何从A中选择10条记录,从B中选择10条记录?

If both the tables have same number and type of columns. 如果两个表的列数和类型相同。 Then use union or union all 然后使用并集或全部并集

Select col1,col2,col3 from table1
union
Select col1,col2,col3 from table2

Something like that? 这样的东西?

    SELECT col1,col2,col3
    FROM TABLE_A

    UNION 

    SELECT TOP (SELECT COUNT(*) FROM TABLE_A) col1,col2,col3
    FROM TABLE_B

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

相关问题 如何在一个MySQL查询中从两个不同的表中选择记录? - How can I select records from two different tables in one MySQL query? 如何从SQL Server Management Studio中的两个不同服务器和数据库中选择同一查询中的数据? - How can I select data in the same query from two different servers and databases from SQL Server Management Studio? 从两个不同的数据库(同一服务器)中选择 2 个不同的表的 SQL 语句 - SQL statement to select from 2 different tables, from two different databases (same server) 如何在 select 同一列具有两个不同的条件 SQL 服务器? - How to can select the same column with two different condition SQL Server? 从两个不同的表但相同的数据库中选择 SQL 查询 - Select SQL query from two different tables but same database SQL Select 来自两个不同表的相同列 - SQL Select same columns from two different tables 从两个不同的表中选择不同的记录 - Select distinct records from two different tables 如何在SQL Server中通过选择查询从100条记录中获取第n条记录? - How do I get nth number of records from 100 records by select query in SQL Server? SQL 从两个表中选择不同的记录 - SQL Select Distinct Records From Two Tables 如何从与FK链接的两个不同表中删除记录? SQL - How to delete records from two different tables that are linked with FK? SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM