考试报名
考试报名
考试内容
考试大纲
在线客服
返回顶部

备考刷题,请到

CDA认证小程序

表A(key,v1),表B(key,v2),其中连接的公共字段key。假设A为左表,下面代码中能实现左反连接的是( )
A. select * from A left join B on A.key=B.key where v2 is null;
B. select * from A left join B using(key) where v2 is not null;
C. select * from A,B where A.key=B.key and v1 is null;
D. Select * from A left join B using(key) where v1 not in (select v1 from A inner join B using(key))
上一题
下一题
收藏
点赞
评论
题目解析
题目评论(0)

先用左连接(左外连接)查询,再判断右表的字段是null值,或者判断左表的字段不在内连接的该字段数据集中。

正确答案是:A: `select * from A left join B on A.key=B.key where v2 is null;`

### 专业分析:

**左反连接(Left Anti Join)**:
左反连接的目的是从左表中选择那些在右表中没有匹配记录的行。换句话说,就是从左表中选择那些在连接字段上没有对应右表记录的行。

**选项分析**:

A: `select * from A left join B on A.key=B.key where v2 is null;`
- 这是标准的左反连接的实现方式。通过左连接将所有左表(A)中的记录保留,并用右表(B)中的匹配记录填充。如果右表中没有匹配的记录,右表的字段(例如v2)将为null。通过`where v2 is null`条件筛选出这些没有匹配记录的行。

B: `select * from A left join B using(key) where v2 is not null;`
- 这个查询会选择左表和右表中有匹配记录的行,因为条件是`where v2 is not null`,这与左反连接的定义相反。

C: `select * from A,B where A.key=B.key and v1 is null;`
- 这是一个内连接(Cartesian Join)的形式,并且条件是`v1 is null`,这与左反连接无关。

D: `Select * from A left join B using(key) where v1 not in (select v1 from A inner join B using(key))`
- 这个查询试图通过子查询来实现左反连接,但由于子查询是内连接,且外部查询条件是`where v1 not in (subquery)`,这个方法虽然理论上可能实现左反连接,但其效率和复杂性都较高,不如选项A直接。

因此,选项A最符合左反连接的定义和实现方式。