连接没有重复行/ NULL值的表

时间:2017-08-09 17:41:56

标签: mysql join union

所以我有多个表,我必须在查询时合并,但joinunion似乎没有实现我的对象

联:

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的新手,非常感谢您的建议和建议

1 个答案:

答案 0 :(得分: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)相同,并且对于彼此相关的行是相同的。 (顺便说一句,这些也应该是相同的数据类型或查询可能需要很长时间才能执行)。我在NAME表中添加了awsaccount列,以便在此上下文中设置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表是某些用户以前的accout雕像的历史记录
  • 为此,我添加了一个DATE字段,可用于控制此表中条目的顺序

这意味着我们可以在JOIN字段上NAME,使用ON子句中的子查询(表C)仅返回来自statusTable的匹配项条目是最新的。由于您的示例中的数据包含NULL上的LEFT列,因此我在这里使用了RIGHT OUTER JOIN,这是在JOINS中遇到的不太常见的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) 我碰到的代码。基本上它说返回第二个表(B)中的所有数据和来自第一个(A)的任何匹配,否则这些列是NULL

结果如下:

JOIN

正如您提到的SQL新手,这里有一个图像可以直观地解释import networkx as nx G = nx.path_graph(5) path = nx.single_source_shortest_path_length(G ,source=src_vertex,cutoff=2) 的不同种类(取自here)。

enter image description here

此致

詹姆斯