snow888 发表于 2011-03-02 13:30

求一个 sql 语句

表 test1

create table test1 ( c1    int not null,
                               c2    int,
                               c3    char(8),
                               c4    dec(18,2),
                               constraint pk_test1 primary key (c1));

create table test2 ( c1   int not null,
                              c2char(20),
                               constraint pk_test2 primary key(c1));



test1 的记录如下

1,1,'测试01',1000.00
2,1,'测试02',100010.00
3,0,'测试03',2110.00

test2 的记录如下

1,'参数01'
2,'参数02'

要求获取
test1.c1,test2.c2,test1.c3,test1.c4

获取test2.c2的关联条件是 test1.c2 = test2.c1 。

我的 sql 语句如下:

select test1.c1,test2.c2,test1.c3,test1.c4 from test1,test2 where test1.c2=test2.c1

问题是这样的语句,我只能展示出 test1 的两条记录,而我实际需要的是使用类似于 left jion 得到的结果,即如果在 test2 中没有满足条件 test1.c2=test2.c1 的结果的时候,在检索的结果集中将 test2.c2 置成空,要求将 test1 表中的三条记录都检索出来。

net_lin 发表于 2011-03-06 15:56

那就用left join呀,select * from test1 left join test2 on test1.c2=test2.c1
页: [1]
查看完整版本: 求一个 sql 语句