Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1880
  • 博文数量: 2
  • 博客积分: 100
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-14 11:13
文章分类

全部博文(2)

文章存档

2010年(2)

我的朋友
最近访客

分类: Mysql/postgreSQL

2010-04-14 12:13:19

user表如下:id  name  street  sex  age  记录用户信息
pay表如下:no  aid    amount               记录用户的费用
其中id和no是主键,aid是外键

select * from user where id = (select aid from pay where no=3);
该子查询只返回一行,故不会报错。
如果子查询返回多行,则报错,比如:
select * from user where id = (select aid from pay where no<4);
报错如下:ERROR 1242 (21000): Subquery returns more than 1 row

改正办法:

使用any
select * from user where id = any (select aid from pay where no<4);

使用 in
select * from user where id in  (select aid from pay where no<4);

使用 exists
select * from user where exists  (select * from pay where pay.aid=user.id and  no<4);

阅读(327) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:用例

给主人留下些什么吧!~~