码迷,mamicode.com
首页 > 其他好文 > 详细

LEFT JOIN个别问题

时间:2019-01-08 15:16:22      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:join   where   表关联   数据量   record   school   time   数据   sele   

SELECT
a.loginuser,
a.schoolid,
count(b.id)
FROM
vhs_school AS a
LEFT JOIN vhs_attence AS b ON a.schoolid = b.schoolid
WHERE
b.recordtime > 1546791470
AND b.recordtime < 1546874270
GROUP BY
a.schoolid

SELECT
a.loginuser,
a.schoolid,
count(b.id)
FROM
vhs_school AS a
LEFT JOIN vhs_attence AS b ON a.schoolid = b.schoolid
AND b.recordtime > 1546791470
AND b.recordtime < 1546874270
GROUP BY
a.schoolid

前者就算用了left join 但是结果还是没有显示a表中的所有字段,因为b表的筛选条件放到最外层,这样就相当于将a表关联b表又做了一遍筛选,所有丢失了a表中其他的字段

后者已经筛选b表的数据后,数据量会减少,再去关联a

后者也可以用

SELECT
a.loginuser,
a.schoolid,
count(b.id)
FROM
vhs_school AS a
LEFT JOIN (
SELECT
*
FROM
vhs_attence AS b b.recordtime > 1546791470
AND b.recordtime < 1546874270
) b ON a.schoolid = b.schoolid

LEFT JOIN个别问题

标签:join   where   表关联   数据量   record   school   time   数据   sele   

原文地址:https://www.cnblogs.com/wutianfei/p/10238205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!