[SQL ] 不同表格 统计

楼主: lazior (懒人)   2018-04-27 17:43:00
数据库名称:mysql
数据库版本:
内容/问题描述:
有两个表格,一个是user, 一个是post
user:
id account data_order
1 AAA 2
2 BBB 1
3 CCC 3
post:
id user_id content post_time(2018-XX-XX)
1 1 ... 2018-04-25
2 2 ... 2018-04-25
3 3 ... 2018-04-27
4 1 ... 2018-04-27
5 2 ... 2018-04-27
6 1 ... 2018-04-27
7 3 ... 2018-04-27
想要统计某一天(2018-04-27)使用者的留言数量,并依照user的data_order排序
希望结果如下
account post_num
BBB 1
AAA 2
CCC 2
有几个疑问
1.效能是写在一句sql语法比较好,还是先依据data_order排序后,再各别统计比较好?
2.如果是写在一句sql写法这样对吗?
SELECT user.account, count(post.id) AS post_num
FROM user, post
WHERE user.id = post.user_id AND post.post_time = '2018-04-27'
GROUP BY post.user_id
ORDER BY user.data_order
3.用 LEFT JOIN 的话效能会比较好吗?还是其实都差不多?
SELECT user.account, count(post.id) AS post_num
FROM user
LEFT JOIN user ON post.user_id = user.id
WHERE post.post_time = '2018-04-27'
ORDER BY user.data_order
先感谢各位大神了!!

Links booklink

Contact Us: admin [ a t ] ucptt.com