在WHERE IN子查询中选择

时间:2019-04-18 10:46:40

标签: mysql

我有以下查询:

SELECT u.*, m.first_name, m.last_name
FROM unit u
INNER JOIN member m
ON m.id = u.member_id
WHERE u.id in (SELECT p.id FROM property p
               WHERE p.member_id = 1 and p.is_deleted = 0) AND
      u.is_deleted = 0

这不会产生我想要的。如果我单独运行它,那么WHERE IN部分中的子查询(从属性p中选择p.id,其中p.member_id = 1和p.is_deleted = 0)返回1,2,3,6,但是完整的查询看起来像它只是从子查询中得到1?

有人吗?

编辑:附加了以下结果的屏幕:

1)完整查询结果:

enter image description here

2)子查询结果

enter image description here

3)单位表

enter image description here

4)属性表

enter image description here

1 个答案:

答案 0 :(得分:1)

这是您的正确查询。您的错误与IN()子句有关。在下面检查其是否正确。

select u.*, m.first_name, m.last_name 
from unit u 
inner join member m on m.id = u.member_id 
where u.is_deleted = 0 
AND u.property_id IN 
(select pr.id as pids from property pr where pr.member_id = 1 and pr.is_deleted = 0)

检查屏幕截图以获取结果。 enter image description here