34,593
社区成员
发帖
与我相关
我的任务
分享
create table T1(F1 int)
insert into t1 values(1)
insert into t1 values(2)
create table t2(F1 varchar(10))
insert into t2 values('A')
insert into t2 values('B')
insert into t2 values('C')
create table t3(F1 int,F2 varchar(10))
insert into t3 values(1 ,'A')
insert into t3 values(2 ,'A')
insert into t3 values(1 ,'B')
insert into t3 values(2 ,'B')
insert into t3 values(1 ,'C')
insert into t3 select t1.f1 , t2.f1 f2 from t1 , t2 where not exists(select 1 from t3 where t3.f1 = t1.f1 and t3.f2 = t2.f1)
select * from t3
drop table t1 , t2 , t3
/*
F1 F2
----------- ----------
1 A
2 A
1 B
2 B
1 C
2 C
(所影响的行数为 6 行)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-10-29 15:58:34
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[T1]
if object_id('[T1]') is not null drop table [T1]
go
create table [T1]([F1] int)
insert [T1]
select 1 union all
select 2
--> 测试数据:[T2]
if object_id('[T2]') is not null drop table [T2]
go
create table [T2]([F1] varchar(1))
insert [T2]
select 'A' union all
select 'B' union all
select 'C'
--> 测试数据:[T3]
if object_id('[T3]') is not null drop table [T3]
go
create table [T3]([F1] int,[F2] varchar(1))
insert [T3]
select 1,'A' union all
select 2,'A' union all
select 1,'B' union all
select 2,'B' union all
select 1,'C'
--------------开始查询--------------------------
--select * from t1 cross join (select t2.f1 f2 from t2)a
insert into t3 select * from (select * from t1 cross join (select t2.f1 f2 from t2)a) b where not exists(select 1 from t3 where t3.f1 =b.f1 and t3.f2 = b.f2)
select * from t3
----------------结果----------------------------
/* F1 F2
----------- ----
1 A
2 A
1 B
2 B
1 C
2 C
(6 行受影响)
*/
create table T1(F1 int)
insert into t1 values(1)
insert into t1 values(2)
create table t2(F1 varchar(10))
insert into t2 values('A')
insert into t2 values('B')
insert into t2 values('C')
create table t3(F1 int,F2 varchar(10))
insert into t3 values(1 ,'A')
insert into t3 values(2 ,'A')
insert into t3 values(1 ,'B')
insert into t3 values(2 ,'B')
insert into t3 values(1 ,'C')
insert into t3 select * from (select t1.f1 , t2.f1 f2 from t1 , t2) t where not exists(select 1 from t3 where t3.f1 = t.f1 and t3.f2 = t.f2)
select * from t3
drop table t1 , t2 , t3
/*
F1 F2
----------- ----------
1 A
2 A
1 B
2 B
1 C
2 C
(所影响的行数为 6 行)
*/