不知道该如何优化的sql,求教

SELECT distinct(id) as id FROM source WHERE 1  and  price>50   and  price<70  and id not in (SELECT id FROM series WHERE tid=5 or tid=11)

该语句就是找出两种类型的商品,报价还是在50到70中间的商品id唯一列表

求指教!谢谢!!!

阅读 1.5k
1 个回答

思路就是用LEFT JOIN, 因为MySQL的子查询性能很差

SELECT distinct(t1.id) as id
FROM
   source    t1
LEFT JOIN 
   series  t2 
   on t1.id=t2.id
WHERE price>50 and price<70
   and (t2.tid=5 or t2.tid=11)
   and t2.id   is null

瞎写的,不见得对。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题