2017-02-26 145 views
0

我有兩個表如下mysql的UNION ALL,ORDER BY,LIMIT和INNER 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);

但沒有得到預期的記錄。請提出要做的更改。

+0

這是複雜的MySQL的,因爲它既滯後標準SQL的' ROW_NUMBER'和'CROSS APPLY'。這些都可以輕鬆解決這個問題。最好的辦法是查看如何在MySQL中使用變量來模擬'ROW_NUMBER',以便每組獲得n條記錄,因爲每個用戶名和類型需要5條記錄。 –

+0

好吧,這並不複雜,但請參閱http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-一個非常簡單的sql查詢 – Strawberry

回答

0

您有用戶USERNAME欄名稱,而不是登錄名和你內解體後參照表T3 ALL(而不是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) 
+0

做了建議的更改後,我得到這個錯誤1248(42000):每個派生表必須有自己的別名。 – Murali

+0

答案已更新.. – scaisEdge

+0

獲取相同的錯誤 – Murali