php - 三张表,联表查询,这个sql还能再优化吗

查看:220
本文介绍了php - 三张表,联表查询,这个sql还能再优化吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

SELECT c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs
FROM (wb_userinfo c LEFT JOIN wb_weibo a ON c.uid = a.uid)
LEFT JOIN wb_picture b ON a.id = b.wid WHERE a.uid IN(SELECT follow FROM wb_follow WHERE fans = 27) OR a.uid = 27 ORDER BY a.times LIMIT 0,10;

解决方案

有用or的可以考虑换成用union来尝试下

(SELECT c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs
FROM (wb_userinfo c LEFT JOIN wb_weibo a ON c.uid = a.uid)
LEFT JOIN wb_picture b ON a.id = b.wid WHERE a.uid IN(SELECT follow FROM wb_follow WHERE fans = 27) )
union
(SELECT c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs
FROM (wb_userinfo c LEFT JOIN wb_weibo a ON c.uid = a.uid)
LEFT JOIN wb_picture b ON a.id = b.wid WHERE a.uid = 27 )
ORDER BY a.times LIMIT 0,10;

这篇关于php - 三张表,联表查询,这个sql还能再优化吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆