大伙来写一条sql语句

snowerhuayun 2003-10-16 06:45:55
两个表:tableA,tableB
tableA的两个字段:a1,a2
tableB的两个字段:b1,b2

题目:
1、列出tableA中 a1=b1 并且a2=b2 的所有记录
2、列出tableA中 a1=b1 并且a2=b2 之外的所有记录

举例说明:
tableA
a1 a2
1 2
3 4
5 6
7 8

tableB
b1 b2
1 2
3 4
9 10

题目1要求得到的结果是:
1 2
3 4

题目2要求得到的结果是:
5 6
7 8
...全文
69 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowerhuayun 2003-10-16
  • 打赏
  • 举报
回复
To 马可:
别笑话我啊
看我是怎么写的:

select * from tablea where tableA.a1 not in(select tablea.a1 from tablea,tableb where tablea.a1=tableb.b1 and tablea.a2=tableb.b2) or
(tablea.a1 in(select tablea.a1 from tablea,tableb where tablea.a1=tableb.b1 and tablea.a2=tableb.b2) and
tablea.a2 not in (select tablea.a2 from tablea,tableb where tablea.a1=tableb.b1 and tablea.a2=tableb.b2))

哈哈哈哈哈....
我被自己笑死了。

笑毕,谢谢!


txlicenhe 2003-10-16
  • 打赏
  • 举报
回复
create table tableA (a1 int,a2 int)
insert tableA select 1,2
Union all select 3,4
Union all select 5,6
Union all select 7,8
create table tableB(b1 int,b2 int)
insert tableB select 1,2
Union all select 3,4
Union all select 9,10
1:
Select a.* from tableA a join tableB b on a.a1 = b.b1 and a.a2 = b.b2
a1 a2
----------- -----------
1 2
3 4

(所影响的行数为 2 行)

2:
Select * from tableA a where not exists(select * from tableB where b1 = a.a1 and b2 = a.a2)

a1 a2
----------- -----------
5 6
7 8

(所影响的行数为 2 行)

seraph2 2003-10-16
  • 打赏
  • 举报
回复
1/ select tableA.* from tableA,tableB where tableA.a1=tableB.b1 and tableA.a2=tableB.b2
2/ select tableA.* from tableB,tableB where tableA.a1<>tableB.b1 and tableA.a2<>tableB.b2
语句无错误。
snowerhuayun 2003-10-16
  • 打赏
  • 举报
回复
or也不对
snowerhuayun 2003-10-16
  • 打赏
  • 举报
回复
To seraph2:
兄第二题答的不对。
结果是:
3 4
5 6
7 8
1 2
5 6
7 8
1 2
3 4
hdvcd 2003-10-16
  • 打赏
  • 举报
回复
不同意第二句,用or,不是用and
online 2003-10-16
  • 打赏
  • 举报
回复
同意楼上
seraph2 2003-10-16
  • 打赏
  • 举报
回复
1/ select * from tableA,tableB where tableA.a1=tableB.b1 and tableA.a2=tableB.b2
2/ select * from tableB,tableB where tableA.a1<>tableB.b1 and tableA.a2<>tableB.b2
多看两眼书什么都出来了!

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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