所以我有,我有合併時查詢但兩者join
和union
似乎並不去實現我的目標是什麼連接表沒有重複的行/ 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,並會感謝您的意見和建議
你如何在你的表匹配一行到其他?一旦你有了答案,在連接中查找ON子句。 – dev8080
通常不會抑制在SQL中重複的列值。這通常在表示層完成。爲什麼?因此,如果以後對數據執行導入,則NULL值將與適當的名稱/狀態相匹配;如果預期人們可以按名稱排序並且不會搞砸數據。但是,如果你一定可以使用用戶變量和case語句來完成https://stackoverflow.com/questions/22354754/how-to-write-a-query-to-suppress-consecutive-repeating-values-in-resultset -colum – xQbert