2017-08-09 56 views
0

所以我有,我有合併時查詢但兩者joinunion似乎並不去實現我的目標是什麼連接表沒有重複的行/ NULL值

聯盟的多個表:

mysql> SELECT NAME, currentstatus, NULL AS username, NULL AS status FROM statustable WHERE Name like '%ahsan%' UNION ALL SELECT NULL AS NAME, NULL AS currentstatus, username, status from awsaccount WHERE username like '%ahsan%'; 
+--------------+-------------------+----------+--------------------------------+ 
| NAME   | currentstatus  | status | username       | 
+--------------+-------------------+----------+--------------------------------+ 
| Ahsan Naseem | activated   | NULL  | NULL       | 
| NULL   | NULL    | ACTIVE | ahsan.naseem  | 
| NULL   | NULL    | INACTIVE | ahsantest      | 
| NULL   | NULL    | ACTIVE | ahsantest2      | 
| NULL   | NULL    | ACTIVE | ahsantestuserprofileupdate  | 
| NULL   | NULL    | INACTIVE | ahsantestuserprofileupdate_cli | 
| NULL   | NULL    | INACTIVE | decent.ahsan   | 
| NULL   | NULL    | INACTIVE | decent.ahsan_cli    | 
+--------------+-------------------+----------+--------------------------------+ 
8 rows in set (0.00 sec) 

加入:

mysql> select * from (select * from statustable where Name like '%ahsan%') as ab JOIN (select * from awsaccount where username like '%ahsan%') as asb;                  
    +--------------+-------------------+--------------------------------+----------+ 
    | Name   | currentstatus | username      | status | 
    +--------------+-------------------+--------------------------------+----------+ 
    | Ahsan Naseem | activated   | ahsan.naseem     | ACTIVE | 
    | Ahsan Naseem | activated   | ahsantest      | INACTIVE | 
    | Ahsan Naseem | activated   | ahsantest2      | ACTIVE | 
    | Ahsan Naseem | activated   | ahsantestuserprofileupdate  | ACTIVE | 
    | Ahsan Naseem | activated   | ahsantestuserprofileupdate_cli | INACTIVE | 
    | Ahsan Naseem | activated   | decent.ahsan   | INACTIVE | 
    | Ahsan Naseem | activated   | decent.ahsan_cli    | INACTIVE | 
    +--------------+-------------------+--------------------------------+----------+ 
    7 rows in set (0.00 sec) 

我想實現的是:

+--------------+-------------------+----------+--------------------------------+ 
| NAME   | currentstatus | status | username      | 
+--------------+-------------------+----------+--------------------------------+ 
| Ahsan Naseem | Active   | ACTIVE | ahsan.naseem  | 
| NULL   | NULL    | INACTIVE | ahsantest      | 
| NULL   | NULL    | ACTIVE | ahsantest2      | 
| NULL   | NULL    | ACTIVE | ahsantestuserprofileupdate  | 
| NULL   | NULL    | INACTIVE | ahsantestuserprofileupdate_cli | 
| NULL   | NULL    | INACTIVE | decent.ahsan  | 
| NULL   | NULL    | INACTIVE | decent.ahsan_cli    | 
+--------------+-------------------+----------+--------------------------------+ 

這真的有可能嗎?我是新來的sql,並會感謝您的意見和建議

+2

你如何在你的表匹配一行到其他?一旦你有了答案,在連接中查找ON子句。 – dev8080

+1

通常不會抑制在SQL中重複的​​列值。這通常在表示層完成。爲什麼?因此,如果以後對數據執行導入,則NULL值將與適當的名稱/狀態相匹配;如果預期人們可以按名稱排序並且不會搞砸數據。但是,如果你一定可以使用用戶變量和case語句來完成https://stackoverflow.com/questions/22354754/how-to-write-a-query-to-suppress-consecutive-repeating-values-in-resultset -colum – xQbert

回答

1

請在將來提供表格定義和示例數據,這使得您的問題很可能會很快得到有用的答案。下面是我做了這些目的有一些變化的數據,讓我來回答你的問題:

DROP TABLE IF EXISTS statustable; 
CREATE TABLE statustable(`NAME` VARCHAR(32), currentStatus VARCHAR(32)); 

INSERT INTO statustable VALUES('Ahsan Naseem', 'activated'); 

DROP TABLE IF EXISTS awsaccount; 
CREATE TABLE awsaccount(`NAME` VARCHAR(32), username VARCHAR(32), `status` VARCHAR(32), logEntry DATETIME); 

INSERT INTO awsaccount VALUES('Ahsan Naseem', 'ACTIVE', 'ahsan.naseem', '2000-01-07'), 
    ('Ahsan Naseem', 'INACTIVE', 'ahsantest', '2000-01-06'), 
    ('Ahsan Naseem', 'ACTIVE', 'ahsantest2', '2000-01-05'), 
    ('Ahsan Naseem', 'ACTIVE', 'ahsantestuserprofileupdate', '2000-01-04'), 
    ('Ahsan Naseem', 'INACTIVE', 'ahsantestuserprofileupdate_cli', '2000-01-03'), 
    ('Ahsan Naseem', 'INACTIVE', 'decent.ahsan', '2000-01-02'), 
    ('Ahsan Naseem', 'INACTIVE', 'decent.ahsan_cli', '2000-01-01'); 

據我可以從你的樣品告訴查詢這裏有兩個問題,首先有沒有按在兩個表中似乎都不是公用密鑰,這是兩個表中與要匹配的行(連接/ JOIN)相同的值,對於彼此相關的行是相同的。 (順便說一下,這些數據類型也應該是相同的,否則查詢可能需要很長時間才能執行)。我在awsaccount表中添加了一個NAME列,使JOIN成爲可能。從這個結果像這樣:

SELECT A.`NAME`, A.currentStatus, B.username, B.`status` FROM 
    statusTable A 
RIGHT OUTER JOIN 
    awsaccount B 
ON A.`NAME` = B.`NAME`; 

+--------------+---------------+----------+--------------------------------+ 
| NAME   | currentStatus | username | status       | 
+--------------+---------------+----------+--------------------------------+ 
| Ahsan Naseem | activated  | ACTIVE | ahsan.naseem     | 
| Ahsan Naseem | activated  | INACTIVE | ahsantest      | 
| Ahsan Naseem | activated  | ACTIVE | ahsantest2      | 
| Ahsan Naseem | activated  | ACTIVE | ahsantestuserprofileupdate  | 
| Ahsan Naseem | activated  | INACTIVE | ahsantestuserprofileupdate_cli | 
| Ahsan Naseem | activated  | INACTIVE | decent.ahsan     | 
| Ahsan Naseem | activated  | INACTIVE | decent.ahsan_cli    | 
+--------------+---------------+----------+--------------------------------+ 
7 rows in set (0.00 sec) 

更接近您所需要的,但不能令人信服,因爲NAME關鍵是常見的兩個表中的所有條目。爲了產生一個輸出在你的問題中幾個步驟是必要的,我做了以下假設:

  • awsaccout表是某種用戶的歷史,以前的戶頭雕像
  • 爲此我已經添加了DATE字段可以被用來控制條目的順序進入該表

這意味着我們可以JOINON子句只返回在NAME字段,使用子查詢(表C)來自的比賽在條目最新的情況下。由於您示例中的數據包含LEFT上的NULL列,因此我在此處使用了一個RIGHT OUTER JOIN,這是我遇到的代碼中遇到的不常見的JOINS之一。基本上,它說:回所有從第二個表(B)和第一(A)任何匹配的數據,否則,這些列是NULL

下面是結果:

SELECT A.`NAME`, A.currentStatus, B.username, B.`status` FROM 
    statusTable A 
RIGHT OUTER JOIN 
    awsaccount B 
ON A.`NAME` = B.`NAME` AND B.logEntry = (SELECT MAX(logEntry) FROM awsaccount WHERE `NAME` = A.`Name`); 

+--------------+---------------+----------+--------------------------------+ 
| NAME   | currentStatus | username | status       | 
+--------------+---------------+----------+--------------------------------+ 
| Ahsan Naseem | activated  | ACTIVE | ahsan.naseem     | 
| NULL   | NULL   | INACTIVE | ahsantest      | 
| NULL   | NULL   | ACTIVE | ahsantest2      | 
| NULL   | NULL   | ACTIVE | ahsantestuserprofileupdate  | 
| NULL   | NULL   | INACTIVE | ahsantestuserprofileupdate_cli | 
| NULL   | NULL   | INACTIVE | decent.ahsan     | 
| NULL   | NULL   | INACTIVE | decent.ahsan_cli    | 
+--------------+---------------+----------+--------------------------------+ 
7 rows in set (0.00 sec) 

正如你所提到的你的SQL新手,這裏是一張圖片,用於直觀地解釋不同種類的JOIN(取自here)。

enter image description here

問候,

詹姆斯

+0

感謝您的詳細描述 –