菜鸟的一个SQL语句写不出了

wenbo234 2007-04-29 10:11:25
SQL测试题 
数据库中有两个表tableA和tableB,他们都有一个int型的字段id,请写出一个sql语句,实现查询id在tableA中有,而tableB中没有的记录
例如tableA中有如下记录
----------------------------
tableA.id tableA.name
3 Mary
4 Jack
5 Lily
----------------------------
tableB.id
3
5
查询的结果应该是
----------------------------
tableA.id tableA.name
4 Jack

这个SQL 语句怎么写
...全文
328 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangshuiyoudu 2007-04-30
  • 打赏
  • 举报
回复
对了,忘了还有一句要告诉你
总有一天你会在我们这些白痴面前变成白痴
等着瞧吧
一定会超过你的
xiangshuiyoudu 2007-04-30
  • 打赏
  • 举报
回复
gjchjw(小草)
怎么能这么说呢?
要是能给出分谁会不给啊
真是的
就你不是白痴!算你是黑痴行了吧
我也是新手
也有很多白痴问题
难道就因为问题对你们来说很白痴我们就不能问了吗?
鄙视你!超级鄙视!
xiangshuiyoudu 2007-04-30
  • 打赏
  • 举报
回复
Loveanshy() 你的好像不行
gjchjw 2007-04-30
  • 打赏
  • 举报
回复
我是来鄙视0分帖的,问的还是白痴问题
xiao_yi 2007-04-30
  • 打赏
  • 举报
回复
not in,exists,any

呵,解法多多哦。
shuai45 2007-04-30
  • 打赏
  • 举报
回复
楼上几位的都8错
我就不写了,偶在这里学习一下写的好的吧~~
Clark21 2007-04-30
  • 打赏
  • 举报
回复
select tableA.id,ableA.name
from tableA,tableB
where tableB.id not in(select tableA.id from tableA)
  • 打赏
  • 举报
回复
一般的处理方式是使用not in,但是如果数据量比较大的话,最好使用exist,性能好一些!
Loveanshy 2007-04-30
  • 打赏
  • 举报
回复
>tableA.id tableA.name
> 3 Mary
>4 Jack
>5 Lily
> ----------------------------
> tableB.id
> 3
> 5
> 查询的结果应该是
> ----------------------------
> tableA.id tableA.name
> 4 Jack
>
>这个SQL 语句怎么写

> >用not in 加子查询就可以了

我看看。用not in 可不可以!
1> select tableB.id from tableB;
查找结果:
3
5

2> select tableA.id,tableA.name from tableA where tableA.id in (3,5);

3> select tableA.id,tableA.name from tableA where tableA.id = 3 or tableB.id = 5

2!> select tableA.id,tableA.name from tableA where tableA.id not in (3,5);

3!> select tableA.id,tableA.name from tableA where tableA.id not= 3 and tableB.id not = 5

查找结果: 可以!
tableA.id tableA.name
4 Jack

做人要厚道!哈哈!




gj_zkb 2007-04-30
  • 打赏
  • 举报
回复
用连接最好
select * from tablea aa left join tableb bb on(aa.id=bb.id) where bb.id is null
wenbo234 2007-04-30
  • 打赏
  • 举报
回复
这么简单 哎没想到呀 我太菜了
radarhp 2007-04-29
  • 打赏
  • 举报
回复
用not in 加子查询就可以了
Loveanshy 2007-04-29
  • 打赏
  • 举报
回复
1.
select tableA.id,tableA.name from tableA,tableB
where tableA.id <> tableB.id;

2.
select tableA.id,tableA.name from tableA
where tableA.id not any (select tableB.id from tableB );

3.
select tableA.id,tableA.name from tableA
where tableA.id <>any (select tableB.id from tableB );

以上,请确认!

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧