1. 程式人生 > >[MySQL]使用EXISTS代替IN

[MySQL]使用EXISTS代替IN

sele from spa span code col select not color

#IN -> EXISTS
SELECT * FROM CouponItem AS A WHERE A.CouponId IN(SELECT B.Id FROM Coupon AS B WHERE B.Id<30);
SELECT * FROM CouponItem AS A WHERE EXISTS(SELECT B.Id FROM Coupon AS B WHERE B.Id<30 AND A.CouponId=B.Id);

#NOT IN -> NOT EXISTS
SELECT * FROM CouponItem AS A WHERE A.CouponId NOT
IN(SELECT B.Id FROM Coupon AS B WHERE B.Id<30); SELECT * FROM CouponItem AS A WHERE NOT EXISTS(SELECT B.Id FROM Coupon AS B WHERE B.Id<30 AND A.CouponId=B.Id);

[MySQL]使用EXISTS代替IN