table a:
col1 col2 col3
CON1 A A1
CON2 A A1
CON3 A A2
TABLE B:
col1 col2
A A1
A A2
A A3
结果:
col1 col2 col3
CON1 A A1
CON1 A A2
CON1 A A3
CON2 A A1
CON2 A A2
CON2 A A3
CON3 A A1
CON3 A A2
CON3 A A3
...全文
1744打赏收藏
sql怎么写?
table a: col1 col2 col3 CON1 A A1 CON2 A A1 CON3 A A2 TABLE B: col1 col2 A A1 A A2 A A3 结果: col1 col2 col3 CON1 A A1 CON1 A A2 CON1 A A3 CON2 A A1 CON2 A A2 CON2 A A3 CON3 A A1 CON3 A A2 CON3 A A3
if object_id('pubs..A') is not null
drop table A
go
create table A(col1 varchar(10),col2 varchar(10),col3 varchar(10))
insert into A(col1,col2,col3) values('CON1', 'A', 'A1')
insert into A(col1,col2,col3) values('CON2', 'A', 'A1')
insert into A(col1,col2,col3) values('CON3', 'A', 'A2')
go
if object_id('pubs..B') is not null
drop table B
go
create table B(col1 varchar(10),col2 varchar(10))
insert into B(col1,col2) values('A', 'A1')
insert into B(col1,col2) values('A', 'A2')
insert into B(col1,col2) values('A', 'A3')
go
select A.col1,A.col2,B.col2 from A,B where A.col2 = B.col1 order by A.col1
drop table A,B
/*
col1 col2 col2
---------- ---------- ----------
CON1 A A1
CON1 A A2
CON1 A A3
CON2 A A1
CON2 A A2
CON2 A A3
CON3 A A1
CON3 A A2
CON3 A A3