mysql 'IN' 子句问题 [英] mysql 'IN' clause problems

查看:24
本文介绍了mysql 'IN' 子句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张如下表,

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); 

以上查询返回以下结果集,

The above query is returning the following result set,

1、2、3

但我需要 1,2,3,4.我的查询哪里出错了.?请帮我.

But I need 1,2,3,4. Where my query is going wrong.? Please help me.

推荐答案

为什么不使用联接?那个查询看起来很丑而且没有优化,但是,我仍然不知道为什么它不起作用.使用与此类似的连接...

Why don't you use a join instead? That query looks ugly and not optimized, yet, I still don't know why it's not working. Use a join similar to this one...

select orange_id from Orange
inner join Apple on Apple.id = Orange.apple_id
where Apple.type = 3

希望有帮助狮子座

这篇关于mysql 'IN' 子句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆