【发布时间】:2013-12-10 07:27:21
【问题描述】:
我有两张如下表,
Table Apple:
----------------------------------------
id | type |
----------------------------------------
1 | 3 |
----------------------------------------
2 | 3 |
----------------------------------------
3 | 3 |
----------------------------------------
Table Orange:
-------------------------------------------
orange id | apple id |
-------------------------------------------
1 | 1 |
-------------------------------------------
2 | 2 |
-------------------------------------------
3 | 3 |
-------------------------------------------
4 | 1 |
--------------------------------------------
4 | 2 |
--------------------------------------------
4 | 3 |
--------------------------------------------
我的查询如下所示,
SELECT orange_id
FROM orange
WHERE apple_id IN (SELECT id
FROM apple
WHERE type = 3);
以上查询返回以下结果集,
1、2、3
但我需要 1,2,3,4。我的查询哪里出错了。?请帮我。
【问题讨论】:
-
如果表变得更大,这不是执行该查询的好方法。
-
您确定结果吗? def 应该返回 6 行...
-
有了这些数据,您的查询应该返回 1,2,3,4,4,4
-
尝试使用
select distinct orange_id from Orange where apple_id in ( select id from Apple where type= 3 );得到1,2,3,4