IT分销/经销数据库

求一个 sql 语句

表 test1create table test1 ( c1    int not null,                               c2    int,         &n...显示全部


表 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,
                                c2  char(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 表中的三条记录都检索出来。
收起
参与7

查看其它 5 个回答dengchen0504的回答

dengchen0504dengchen0504数据库管理员联信永益
你不是都知道用left join了,为啥还不知道写呢?
select
    test1.c1,test2.c2,test1.c3,test1.c4
from test
left outer join
     test2
on test1.c2=test2.c1
2011-03-03
浏览758

回答者

dengchen0504
数据库管理员联信永益
擅长领域: 服务器Linux数据库

dengchen0504 最近回答过的问题

回答状态

  • 发布时间:2011-03-03
  • 关注会员:1 人
  • 回答浏览:758
  • X社区推广