Heim >Backend-Entwicklung >PHP-Tutorial >Drei Tabellen, gemeinsame Tabellenabfrage, kann dieses SQL weiter optimiert werden?
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;
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来尝试下
<code>(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;</code>
使用建议是拆成简单查询来.
explain
PS:尽量避免在sql里出现
or