- 原join 连接语句
SELECT
SUM(video_flowers.number) AS num,
video_flowers.flower_id,
flowers.title,
flowers.image
FROM
`video_flowers`
JOIN
`flowers` ON `video_flowers`.`flower_id` = `flowers`.`id`
JOIN
`video_posts` ON `video_flowers`.`video_post_id` = `video_posts`.`id`
WHERE
`video_posts`.`user_id` = 36
GROUP BY `video_flowers`.`flower_id`
可以优化成
SELECT
vf.num, flowers.title, flowers.imageFROM`flowers`
<span >join</span></br>
(<span >SELECT</span> </br>
<span >SUM</span>(video_flowers.<span >number</span>) <span >AS</span> <span >num</span>, video_flowers.flower_id, video_flowers.video_post_id
<span >FROM</span></br>
video_flowers </br>
<span >GROUP</span> <span >BY</span> <span >`video_flowers`</span>.<span >`flower_id`</span>) <span >AS</span> vf <span >ON</span> <span >`vf`</span>.<span >`flower_id`</span> = <span >`flowers`</span>.<span >`id`</span></br>
<span >join</span> <span >`video_posts`</span> <span >on</span> <span >`video_posts`</span>.<span >`id`</span> = vf.<span >`video_post_id`</span></br>
<span >where</span> video_posts.user_id = <span >36</span>;</code></pre>
这样就没有using filesort 和using temporary