将两个不同查询的结果合并到一个查询中

时间:2015-03-17 09:41:20

标签: mysql select

我有一个包含登录日志的MySQL表。每个条目包含用户电子邮件,IP地址,时间戳和登录结果(0失败,1成功)。

+------------+------------------+------------+--------+
| ip         | email            | datetime   | result |
+------------+------------------+------------+--------+
| 2130706433 | user1@domain.com | 1426498362 | 0      |
| 2130706433 | user1@domain.com | 1426498363 | 1      |
| 2130706433 | user1@domain.com | 1426498364 | 0      |
| 1134706444 | user2@domain.com | 1426498365 | 0      |
+------------+------------------+------------+--------+

我的目标是创建一个唯一查询,以从给定时间戳中提取失败登录的计数,以及user1@domain.com上次登录的时间戳。在这种情况下,我想获得(为简单起见,假设所有条目都在所需的时间戳之后)

+--------+------------+
| count  | datetime   |
+--------+------------+
| 3      | 1426498364 |
+--------+------------+

到目前为止,我已经创建了两个单独的查询来分别提取结果

SELECT COUNT(result) as count FROM (SELECT result FROM accesslogs WHERE datetime>1426498360 AND result=0) as subt
SELECT MAX(datetime) as datetime FROM accesslogs WHERE email=`user1@domain.com`

现在我要将它们组合在一起以获得单个查询的结果。我想知道如何使用JOIN声明,但我不知道可以加入查询的列。我该怎么办?

4 个答案:

答案 0 :(得分:1)

您可以使用条件聚合,例如

select 
sum( 
  case 
   when result=0 and datetime>1426498360 
   then  1 else 0 end 
) as `count`,
max(
   case 
   when email = 'user1@domain.com' then datetime end
) as datetime 
from accesslogs ;

这是一个例子

mysql> select * from test ;
+------------+------------------+------------+--------+
| ip         | email            | datetime   | result |
+------------+------------------+------------+--------+
| 2130706433 | user1@domain.com | 1426498362 |      0 |
| 2130706433 | user1@domain.com | 1426498363 |      1 |
| 2130706433 | user1@domain.com | 1426498364 |      0 |
| 1134706444 | user2@domain.com | 1426498365 |      0 |
+------------+------------------+------------+--------+
4 rows in set (0.00 sec)

    mysql> select 
    -> sum( 
    ->   case 
    ->    when result=0 and datetime>1426498360 
    ->    then  1 else 0 end 
    -> ) as `count`,
    -> max(
    ->    case 
    ->    when email = 'user1@domain.com' then datetime end
    -> ) as datetime 
    -> from test ;
+-------+------------+
| count | datetime   |
+-------+------------+
|     3 | 1426498364 |
+-------+------------+
1 row in set (0.00 sec)

答案 1 :(得分:0)

尝试

select * from (selete statement a) a join (selete statement b) b 

答案 2 :(得分:0)

您可以使用UNION ALL但两个查询必须具有相同的列。 我建议在其他列中为每个查询描述类型的值添加text column,如:

SELECT COUNT(result) as count, 'count' AS TYPE FROM (SELECT result FROM accesslogs WHERE datetime>1426498360 AND result=0) as subt

UNION ALL

SELECT MAX(datetime) as datetime, 'max' AS TYPE FROM accesslogs WHERE email='user1@domain.com'

编辑:很抱歉,但我意识到这是你想做的更多(这是t-sql代码,但应该有效)

    SELECT a.email, a.count, b.datetime FROM (
    SELECT COUNT(result) as count, email FROM (SELECT result, email FROM 
    accesslogs AND result=0) as subt
    GROUP BY email) a
    JOIN (
    SELECT MAX(datetime) as datetime , email FROM accesslogs  GROUP BY email) b  
    on a.email = b.email
    WHERE a.email='user1@domain.com'
    AND a.datetime >'2012-12-17' 

答案 3 :(得分:0)

看看MySQL GROUP BYHAVING。看看这是否适合你:

任何电子邮件:

SELECT email, COUNT(resultSet) as loginAttempts, 
COUNT(resultSet) - SUM(resultSet) as failedAttempts
FROM accesslogs 
GROUP BY email;

特定电子邮件:

SELECT COUNT(result) as count, MAX(logDate) FROM accesslogs 
GROUP BY email
HAVING email='user1@example.com';