gpt4 book ai didi

mysql - 计算两个表的行数

转载 作者:行者123 更新时间:2023-11-29 19:12:38 25 4
gpt4 key购买 nike

所以我有这个表

登录

+---------+------------+---------+
| date_id | date_today | user_id |
+---------+------------+---------+
| 1 | 2017/03/19 | 16 |
| 2 | 2017/03/20 | 17 |
| 3 | 2017/03/20 | 12 |
| 4 | 2017/03/21 | 16 |
| 5 | 2017/03/22 | 10 |
| 6 | 2017/03/22 | 11 |
+---------+------------+---------+

文件下载

+---------+------------+---------+
| date_id | date_today | user_id |
+---------+------------+---------+
| 1 | 2017/03/20 | 17 |
| 2 | 2017/03/20 | 17 |
| 3 | 2017/03/20 | 12 |
| 4 | 2017/03/20 | 17 |
| 5 | 2017/03/20 | 12 |
| 6 | 2017/03/20 | 12 |
| 7 | 2017/03/20 | 12 |
| 8 | 2017/03/20 | 12 |
| 9 | 2017/03/22 | 10 |
| 10 | 2017/03/22 | 10 |
| 11 | 2017/03/22 | 11 |
+---------+------------+---------+

这是我想要的结果:

+------------+-----------+--------+
| date_today | login_num | dl_num |
+------------+---------+----------+
| 2017/03/19 | 1 | 0 |
| 2017/03/20 | 2 | 8 |
| 2017/03/21 | 1 | 0 |
| 2017/03/22 | 2 | 3 |
+------------+-----------+--------+

我对 mysql 还很陌生,所以任何帮助将不胜感激。谢谢你! :)

最佳答案

我认为最简单的方法是union allgroup by:

select date_today, sum(login) as logins, sum(dl) as dls
from ((select date_today, 1 as login, 0 as dl from log_in
) union all
(select date_today, 0, 1 from file_downloads
)
) lfd
group by date_today;

关于mysql - 计算两个表的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42966180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com