日期:2014-05-18  浏览次数:20621 次

sql 条件查询"奇异"地方,帮解决啊
SELECT sl.sid FROM modoer_coupons c LEFT JOIN modoer_subjectlink sl ON c.couponid = sl.flagid WHERE city_id IN (0,1) AND sl.flag = 'coupon' ORDER BY c.status LIMIT 1, 10

有结果

SELECT sl.sid FROM modoer_coupons c LEFT JOIN modoer_subjectlink sl ON c.couponid = sl.flagid WHERE city_id IN (0,1) AND sl.sid=732 AND sl.flag = 'coupon' ORDER BY c.status LIMIT 1, 10

没有结果

SELECT sl.sid FROM modoer_coupons c LEFT JOIN modoer_subjectlink sl ON c.couponid = sl.flagid WHERE city_id IN (0,1) AND sl.sid!=732 AND sl.flag = 'coupon' ORDER BY c.status LIMIT 1, 10

有结果

SELECT sl.sid FROM modoer_coupons c LEFT JOIN modoer_subjectlink sl ON c.couponid = sl.flagid WHERE sl.sid='731' ORDER BY c.status LIMIT 1, 10

没有结果

SELECT sl.sid FROM modoer_coupons c LEFT JOIN modoer_subjectlink sl ON c.couponid = sl.flagid WHERE sl.sid!='731' ORDER BY c.status LIMIT 1, 10

有结果
求助啊






------解决方案--------------------
SQL code
SELECT sl.sid FROM modoer_coupons c 
LEFT JOIN modoer_subjectlink sl ON c.couponid = sl.flagid AND sl.flag = 'coupon'
WHERE city_id IN (0,1)  ORDER BY c.status LIMIT 1, 10

------解决方案--------------------
LIMIT 1, 10

是MYSQL的吧。
------解决方案--------------------
ta
id 
1
2

tb
id sid
1 555
3 732

select a.*,b.* from ta a left join tb b on a.id= b.id where b.sid=732
在left join之后
第tb的第二行(id=3)未能连接, ta作为左输入,全部保留, tb第一行右输入会有输出,第二行右输出为null
然后where
b.sid=732, 两行全被过滤, 自然没结果。

其实你的很简单,
不要加sid=732的条件,在navicat mysql客户端中, 仅取左联, 看结果, 或者然后在结果中再继续过滤 sid=732的记录,看看有没有值就是了。