0

我有一张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似乎都将计数加在一起,或者出于某种原因删除了行。有任何想法吗?

4

1 回答 1

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
于 2013-09-29T07:10:51.643 回答