MySQL SQL Union给出了奇怪的结果

时间:2014-03-30 19:42:05

标签: mysql sql

我在MySQL中有表格,我希望获得有关查找3个用户的使用限制的统计数据

  1. 过去一小时内用于登录的不同IP地址(列ip)
  2. 过去一天用户的帖子数量(列cnt)
  3. 过去10分钟内用户的会话数(列authid是用户名)
  4. 由于输入控制对象的原因我需要这三行,所以我开始使用MySQL UNION但是我得到了奇怪的结果。这就是我所拥有的

    mysql> desc senderlimits;
    +----------+--------------+------+-----+-------------------+-------+
    | Field    | Type         | Null | Key | Default           | Extra |
    +----------+--------------+------+-----+-------------------+-------+
    | authid   | varchar(128) | NO   |     | NULL              |       |
    | ip       | varchar(40)  | NO   |     | NULL              |       |
    | cnt      | int(11)      | NO   |     | 1                 |       |
    | datetime | timestamp    | NO   |     | CURRENT_TIMESTAMP |       |
    +----------+--------------+------+-----+-------------------+-------+
    4 rows in set (0.00 sec)
    
    mysql> select count(distinct ip) as ipl from senderlimits where authid='bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 1 hour) UNION select sum(cnt) as cntl from senderlimits where authid='bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 24 hour) UNION select sum(cnt) as vcnt from senderlimits where authid='bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 10 minute) ;
    +------+
    | ipl  |
    +------+
    |    1 |
    |   95 |
    +------+
    2 rows in set (0.04 sec)
    
    mysql> select count(distinct ip) as ipl from senderlimits where authid='cincinnati@psmail.net' and datetime > date_sub(now(),INTERVAL 1 hour);
    +-----+
    | ipl |
    +-----+
    |   0 |
    +-----+
    1 row in set (0.01 sec)
    
    mysql> select sum(cnt) as cntl from senderlimits where authid='bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 24 hour) ;
    +------+
    | cntl |
    +------+
    |   95 |
    +------+
    1 row in set (0.02 sec)
    
    mysql> select count(authid) as vcnt from senderlimits where authid='bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 10 minute);
    +------+
    | vcnt |
    +------+
    |    1 |
    +------+
    1 row in set (0.02 sec)
    
    mysql>
    

    使用UNION选项时,结果非常奇怪。有专家知道为什么吗?或者我如何能够获得相同的结果 0 95 1

    按此顺序?

1 个答案:

答案 0 :(得分:1)

UNION删除重复项。你想要UNION ALL

select count(distinct ip) as ipl
from senderlimits
where authid = 'bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 1 hour)
UNION ALL
select sum(cnt) as cntl
from senderlimits
where authid = 'bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 24 hour)
UNION ALL
select sum(cnt) as vcnt
from senderlimits
where authid = 'bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 10 minute) ;

但是,您不应该依赖于结果的顺序与子查询的顺序相同(这不保证,即使它经常在实践中发生)。您应该包含一个列,指定哪个数字代表什么:

select '1 hour' as which, count(distinct ip) as ipl
from senderlimits
where authid = 'bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 1 hour)
UNION ALL
select '24 hour' as which, sum(cnt) as cntl
from senderlimits
where authid = 'bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 24 hour)
UNION ALL
select '10 minutes' as which, sum(cnt) as vcnt
from senderlimits
where authid = 'bob@nsd.clear.net' and datetime > date_sub(now(),INTERVAL 10 minute) ;