简体   繁体   English

简短但SQL查询(T-SQL,SQL Server)

[英]Short but though SQL Query (T-SQL,SQL Server)

Suppose I have 2 tables, each tables has N columns. 假设我有2个表,每个表都有N列。 There are NO duplicate rows in table1 table1中没有重复的行

And now we want to know what datasets in table2 (including duplicates) are also contained in table1 . 现在我们想知道table1中还包含了table2哪些数据集(包括重复项)。

I tried 我试过了

select * from table1
intersect
select * from table2

But this only gives me unique rows that are in both tables. 但这只给了我两个表中的唯一行。 But I don't want unique rows, are want to see all rows in table2 that are in table1 ... 但是我不想唯一的行,想要看到table2中table1所有行...

Keep in mind!! 记住!! I cannot do 我不能做

select *
from table1 a, table b
where a.table1col = b.table2col

...because I don't know the number of columns of the tables at runtime. ...因为我在运行时不知道表的列数。

Sure I could do something with dynamic SQL and iterate over the column numbers but I'm asking this precisely because it seems too simple a query for that kind of stuff.. 当然,我可以使用动态SQL进行某些操作,并遍历列号,但是我之所以问这个恰恰是因为对于这种东西来说,查询似乎太简单了。

Example: 例:

create table table1 (table1col int)
create table table2 (table2col int)

insert into table1 values (8)
insert into table1 values (7)

insert into table2 values (1)
insert into table2 values (8)
insert into table2 values (7)
insert into table2 values (7)
insert into table2 values (2)
insert into table2 values (9)

I want my query then to return: 我想要我的查询然后返回:

8
7
7

If the amount of columns is not know, you will have to resort to a value computed over a row to make a match. 如果不知道列数,则必须求助于在行上计算出的值以进行匹配。

One such function is CHECKSUM . CHECKSUM就是这样一种功能。

Returns the checksum value computed over a row of a table, or over a list of expressions. 返回在表的一行或表达式列表上计算的校验和值。 CHECKSUM is intended for use in building hash indices. CHECKSUM旨在用于构建哈希索引。

SQL Statement SQL语句

SELECT  tm.*
FROM    (
          SELECT  CS = CHECKSUM(*)
          FROM    Table2
        ) tm          
        INNER JOIN (          
          SELECT  CS = CHECKSUM(*)
          FROM    Table2          
          INTERSECT
          SELECT  CHECKSUM(*)          
          FROM    Table1
        ) ti ON ti.CS = tm.CS

Note that CHECKSUM might introduce collisions. 请注意, CHECKSUM可能会引入冲突。 You will have to test for that before doing any operation on your data. 在对数据进行任何操作之前,您必须先进行测试。

Edit 编辑

In case you are using SQL Server 2005 , you might make this a bit more robust by throwing in HASH_BYTES . 如果您使用的是SQL Server 2005 ,则可以通过抛出HASH_BYTES使其更加健壮。

The downside of HASH_BYTES is that you need to specify the columns on which you want to operate but for all the columns you do known up-front, you could use this to prevent collisions. HASH_BYTES的缺点是,您需要指定要在其上进行操作的列,但是对于您之前已经知道的所有列,可以使用它来防止冲突。

EXCEPT vs INTERSECT - link 除了相交- 链接

EXCEPT returns any distinct values from the left query that are not also found on the right query. EXCEPT从左查询中返回在右查询中也找不到的任何不同的值。

INTERSECT returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand. INTERSECT返回由INTERSECT操作数的左侧和右侧的查询返回的任何不同的值。

Maybe EXCEPT can solve your problem 也许EXCEPT可以解决您的问题

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

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