希望删除子查询

时间:2013-11-19 13:18:11

标签: mysql

此查询适用于我,但我不想使用子查询。我试了很多但是失败了我可以在没有子查询的情况下使用这个查询吗?

select a1.active_id
from active_table a1
where a1.active_id
NOT IN ( select a2.active_id from view a2 group by a2.active_id )
AND datediff(now(),active_date)>30

2 个答案:

答案 0 :(得分:4)

使用LEFT JOIN

SELECT a1.active_id FROM active_table a1 
LEFT JOIN view a2
ON a1.active_id = a2.active_id
WHERE a2.active IS NULL
AND datediff(now(),active_date)>30

LEFT JOIN / IS NULL相当于NOT IN。有关详细说明,请参阅:

What's the difference between NOT EXISTS vs. NOT IN vs. LEFT JOIN WHERE IS NULL?

答案 1 :(得分:3)

试试这个:

select a1.`active_id`
 from active_table a1 
          left join view a2 on (a1.`active_id` = a2.active_id)
where a2.active_id is null
  AND datediff(now(),a1.active_date)>30