比较2个表并将结果存储在第3个表中? [英] compare 2 tables and store result in 3rd table??

查看:82
本文介绍了比较2个表并将结果存储在第3个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,我想用查询比较这两个表(即

" select * from A where where B.key = A.key"),结果将是存储在

第3表(表C)中。这可能吗?如果有必要,我可以为第3个表(C)创建模式

,因为它与表A完全相同。但我更多

感兴趣的是能够将结果集存储到另一个表中。


谢谢。

解决方案

VMI,


你真的不能说:


从A中选择*,其中B.key = A.key


因为你不知道那时B的集合是什么。你是否想要检查另一个表中是否存在密钥?
如果是的话那么

我会这样做:


选择

a。*

来自

a

内部联接b on a.key = b.key


因为您使用字段名称key ,我假设每个值都是唯一的
,因此存在一对一的关系。


如果执行此选择,它将给你与A相同的结构。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" VMI" < VO ****** @ yahoo.com>在消息中写道

news:eQ ************** @ TK2MSFTNGP14.phx.gbl ...

我有两张桌子我想用查询比较这两个表(即
select * from A where where B.key = A.key),结果将存储在
第3个表中(表C) )。这可能吗?如果有必要,我可以为第3个表(C)创建
模式,因为它与表A完全相同。但是我更感兴趣的是能够将结果集存入另一张
表格。

谢谢。



>从a.key = b.key


a。*
进入c
内部连接b >

你对查询是对的(自从我使用了

sql语句以来已经很久了。)

如果我使用这个语句,我需要在哪里执行它才能比较数据表a和数据表b?两个表都已经填充了

数据,我需要将结果集的数据存储到一个单独的

表中(这是一个克隆)。 />

谢谢。


" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:ei ************** @ TK2MSFTNGP11.phx.gbl ...

VMI,
你不能说:

从A中选择* B.key = A.key

因为你不知道是什么那套B就是那个时候。你是否试图检查另一个表中是否存在密钥?如果是这样,那么我就是这样做的:

选择
a。*
来自
内部联接b。 key = b.key

由于您使用字段名称key,我假设每个值都是唯一的,因此存在一对一的关系。

如果你执行这个选择,它将给你与A相同的结构。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

VMI < VO ****** @ yahoo.com>在消息中写道
新闻:eQ ************** @ TK2MSFTNGP14.phx.gbl ...

我有两个表,我想比较一下这两个表带有查询(即
select * from A where B.key = A.key),结果将存储在第3表(表C)中。这可能吗?如果有必要,我可以为第3个表(C)创建
模式,因为它与表A完全相同。但是我更感兴趣的是能够将结果集存入另一张
表格。

谢谢。




I have two tables and I want to compare these two tables with a query( ie.
"select * from A where B.key = A.key") and the result will be stored in a
3rd table (table C). is this possible? If necessary, I can create the schema
for the 3rd table (C) since it''ll be exactly like table A. But I''m more
interested in being able to store the resulting set into another table.

Thanks.

解决方案

VMI,

You can''t really say:

select * from A where B.key = A.key

Because you don''t know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so, then
I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I''m assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...

I have two tables and I want to compare these two tables with a query( ie.
"select * from A where B.key = A.key") and the result will be stored in a
3rd table (table C). is this possible? If necessary, I can create the
schema for the 3rd table (C) since it''ll be exactly like table A. But I''m
more interested in being able to store the resulting set into another
table.

Thanks.



> select

a.* into c from
a
inner join b on a.key = b.key



You''re right about the query (it''s been a long time since I''ve worked with
sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled with
data, and I need to store the data of the resulting set into a separate
table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ei**************@TK2MSFTNGP11.phx.gbl...

VMI,

You can''t really say:

select * from A where B.key = A.key

Because you don''t know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so,
then I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I''m assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...

I have two tables and I want to compare these two tables with a query( ie.
"select * from A where B.key = A.key") and the result will be stored in a
3rd table (table C). is this possible? If necessary, I can create the
schema for the 3rd table (C) since it''ll be exactly like table A. But I''m
more interested in being able to store the resulting set into another
table.

Thanks.




这篇关于比较2个表并将结果存储在第3个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆