mysql UNION ALL,ORDER BY,LIMIT和INNER JOIN

时间:2017-02-26 20:21:19

标签: mysql join

我有两张表格如下

T1:
+------------+---------------+  
|  Username  |  Role         |  
+------------+---------------+  
|    User1   |  System Admin |  
|    User2   |  Group Admin  |  
|    User3   |  Admin HR     |  
+------------+---------------+ 
T2:
+----+----------+----------------+----------------+-------+  
| Id | Username |       IP       | Authentication | Type  |
+----+----------+----------------+----------------+-------+ 
| 1  | User1    | 10.110.150.111 | Success        | Type1 | 
| 2  | User2    | 10.110.150.121 | Failure        | Type2 |
| 3  | User3    | 10.109.110.110 | Failure        | Type2 |  
| 4  | User1    | 10.110.150.111 | Success        | Type2 |  
| 5  | User1    | 10.110.150.109 | Success        | Type2 |  
| 6  | User4    | 10.25.109.110  | Failure        | Type2 |  
| 7  | NULL     | 10.29.109.110  | Failure        | Type1 |  
| 8  | NULL     | 10.29.209.110  | Failure        | Type2 |  
| 9  | User2    | 10.110.151.116 | Failure        | Type1 |  
| 10 | User2    | 10.110.150.121 | Success        | Type2 |  
| 11 | User5    | 10.105.105.111 | Failure        | Type2 |
+----+----------+----------------+----------------+-------+ 

在表T2中,每个用户可能没有更多记录。我试图根据T1中的用户名从T2获取记录,并且每个匹配T1中用户的用户应该获取5个Type1和5个Type2记录。除了这些应该获取用户名为空/ NULL的记录。

预期输出如下:

+----------+----------------+----------------+-------+  
+ Username |       IP       | Authentication | Type  |
+----------+----------------+----------------+-------+ 
| User1    | 10.110.150.111 | Success        | Type1 |  
| User1    | 10.110.150.111 | Success        | Type2 | 
| User1    | 10.110.150.109 | Success        | Type2 |  
| User2    | 10.110.150.121 | Failure        | Type2 |
| User2    | 10.110.151.116 | Failure        | Type1 |  
| User2    | 10.110.150.121 | Success        | Type2 |
| User3    | 10.109.110.110 | Failure        | Type2 |  
| NULL     | 10.29.109.110  | Failure        | Type1 |  
| NULL     | 10.29.209.110  | Failure        | Type2 | 
+----------+----------------+----------------+-------+ 

尝试使用以下查询:

 (SELECT T3.USERNAME, IP, AUTHENTICATION, TYPE 
    FROM ((SELECT * FROM T2 WHERE TYPE = 'Type1' ORDER BY ID DESC LIMIT 5)  
        UNION ALL 
        (SELECT * FROM T2 WHERE TYPE = 'Type2' ORDER BY ID DESC LIMIT 5)) T3
    INNER JOIN T1 ON T2.LOGINNAME = T1.LOGINNAME) 
  UNION ALL
 (SELECT T2.USERNAME, IP, AUTHENTICATION, TYPE FROM T2 WHERE T2.USERNAME IS NULL); 

但没有得到预期的记录。请建议要做的更改。

1 个答案:

答案 0 :(得分:0)

您拥有用户USERNAME的列名而不是LOGINNAME,而您指的是内部UNION ALL之后的表T3(而不是t2)

(SELECT T3.USERNAME, T3.IP, T3.AUTHENTICATION, T3TYPE 
    FROM (
        SELECT T2.USERNAME, T2.IP, T2.AUTHENTICATION, T2.TYPE  
        FROM T2 WHERE TYPE = 'Type1' ORDER BY ID DESC LIMIT 5 
        UNION ALL 
        SELECT T2.USERNAME, T2.IP, T2.AUTHENTICATION, T2.TYPE  
        FROM T2 WHERE TYPE = 'Type2' ORDER BY ID DESC LIMIT 5 ) T3
    INNER JOIN T1 ON T3.USERNAME = T1.USERNAME )
UNION ALL
(SELECT T2.USERNAME, T2.IP, T2.AUTHENTICATION, T2.TYPE 
FROM T2 WHERE T2.USERNAME IS NULL)