日期:2014-05-18  浏览次数:20535 次

多对多的关联语句如何写
多对多的关联语句如何写,要求显示两个表中的列:
如table1表,table2表
create table_1{
goodsid varchar(20),
a varchar(10),
b varchar(10)
c varchar(10)

};

create table_2{

goodsid varchar(20),
a varchar(10),
f varchar(10)
g varchar(10)

};

table_1中goodsid 为"dj001" 的记录有3条,table_2 中goodsid 为"dj001"的记录有2条,现在用语句把两个表关联以后,显示所有两个表中的记录,
我关联以后,显示的记录不全,或是表中字段有的值显示为空。如表2中是2条记录,表3中有3条记录,则会有一条的列显示为空

我分不多,请大家帮忙了!

------解决方案--------------------
SQL code
select *  from  table_1
union
select *  from  table_2



2.
select *  from  table_1 a full join  table_2  b  on a.goodsid =b.goodsid

------解决方案--------------------
SQL code

select *  
from table_1 t1 FULL OUTER JOIN
     table_2 t2 ON t1.goodsid=t2.goodsid

------解决方案--------------------
SQL code
create table table_1(
goodsid varchar(20),
a varchar(10),
b varchar(10),
c varchar(10)
)

create table table_2(

goodsid varchar(20),
a varchar(10),
f varchar(10),
g varchar(10)
)


insert into table_1 values('dj001','a','b','c')
insert into table_1 values('dj001','a1','b1','c1')
insert into table_1 values('dj001','a2','b2','c2')
insert into table_2 values('dj001',null,'b','c')
insert into table_2 values('dj001','a',null,'c')

select * from table_1 a left join table_2 b on a.goodsid=b.goodsid
select * from table_1 a inner join table_2 b on a.goodsid=b.goodsid
select * from table_1 a full join table_2 b on a.goodsid=b.goodsid
select * from table_1 a right join table_2 b on a.goodsid=b.goodsid
--仔细看看这几个连接结果是一样的

------解决方案--------------------
select * from table_1 cross join table_2