MySQL连续计数2列,并与另一个查询连接

时间:2013-09-29 06:56:13

标签: mysql sql join count union

我有一张像这样的表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                |                |
+--------------+-------------+------+-----+---------------------+----------------+

和表格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 |                |
+------------+-------------+------+-----+---------------------+----------------+

我希望能够计算每个用户在in_username AND 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

但是这只会返回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 | +----------+-----------+----------+ ...如果可能的话,我希望在同一个查询中都有,所以我可以得到这样的结果:

count_in

其中visits.in_username是其用户名在count_out中显示的次数,visits.out_username是其用户名在UNION中出现的次数

我尝试使用{{1}}的所有内容似乎都会将计数添加到一起,或者出于某种原因删除行。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

执行子查询以获取每个总计,将它们与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