[英]MySQL count 2 columns in a row and join with another query
I have a table visits
like this: 我有这样的表visits
:
+--------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| vis_id | int(11) | NO | | NULL | |
| unit | int(11) | NO | | NULL | |
| time_in | timestamp | NO | | CURRENT_TIMESTAMP | |
| time_out | timestamp | NO | | 0000-00-00 00:00:00 | |
| in_username | varchar(16) | NO | | NULL | |
| out_username | varchar(16) | NO | | NULL | |
+--------------+-------------+------+-----+---------------------+----------------+
and a table users
like this: 和这样的表users
:
+------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(32) | NO | | NULL | |
| lname | varchar(32) | NO | | NULL | |
| date_added | timestamp | NO | | CURRENT_TIMESTAMP | |
| username | varchar(16) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| auth_level | int(1) | NO | | 1 | |
| last_login | timestamp | NO | | 0000-00-00 00:00:00 | |
+------------+-------------+------+-----+---------------------+----------------+
I want to be able to count how many times each user is in in_username
AND out_username
... The query I was using before looks like this: 我希望能够计算每个用户在in_username
和 out_username
...我之前使用的查询如下所示:
select count(*) as "count", u.fname as "fname"
from visits v
inner join users as u on u.username = v.in_username
group by u.username order by u.fname
But that only returns how many in_username
's there are... I'd like to have both in the same query if possible, so I could get results like this: 但这只会返回有多少in_username
...如果可能的话,我希望两者都在同一个查询中,所以我可以得到如下结果:
+----------+-----------+----------+
| count_in | count_out | fname |
+----------+-----------+----------+
| 118 | 224 | Bo |
| 27 | 64 | James |
| 147 | 138 | Jeremy |
| 23 | 37 | Jim |
| 182 | 172 | Robert |
| 120 | 158 | Tom |
+----------+-----------+----------+
Where count_in
is how many times their username appears in visits.in_username
, and count_out
is how many times their username appears in visits.out_username
其中count_in
是其用户名出现在visits.in_username
中的visits.in_username
, count_out
是其用户名出现在visits.out_username
中的visits.out_username
Everything I've tried with UNION
seems to add the counts together, or removes rows for some reason. 我尝试使用UNION
似乎都将计数加在一起,或者出于某种原因删除了行。 Any ideas? 有任何想法吗?
Do a subquery to get each total, combine them with UNION
, and then merge them with SUM()
. 进行子查询以获取每个总数,将其与UNION
合并,然后与SUM()
合并。
SELECT SUM(count_in) count_in, SUM(count_out) count_out, fname
FROM (SELECT COUNT(*) count_in, 0 count_out, in_username fname
FROM visits v
GROUP BY fname
UNION
SELECT 0 count_in, COUNT(*) count_out, out_username fname
FROM visits v
GROUP BY fname) combined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.