mysql - 我如何加入这些表并获得所需的结果

标签 mysql sql-server

我有一个包含一些许可证的表,对于不同的许可证,存在基于网络的记录 例如:许可证 12345 有网络记录('101','102','108') 现在我想找出网络('101','102','108','113','114')中,我的许可证丢失的所有网络,之后我们需要复制现有的数据缺少所有五个网络的许可证?

我已经在使用脚本: 看看

select license, count(*) data, network nt1 from x_pns_address
where network  in ('101','102', '108', '113', '114')
and license in ('110438','150275')
group by license, network
into temp sherin_1;

select distinct license, '101' netw from x_pns_address
where license in ('110438','150275')
into temp table1;

insert into  table1 select distinct license, 
'102' netw from x_pns_address
where license in ('110438','150275');
insert into  table1 select distinct license,
'108' netw from x_pns_address
where license in ('110438','150275');
insert into table1  select distinct license, 
'113' netw from x_pns_address
where license in ('110438','150275');
insert into  table1  select distinct license, 
'114' netw from x_pns_address
where license in ('110438','150275');

select distinct a.nt1,a.license,
b.netw  from sherin_1 a, table1 b
where a.license = b.license
and  a.nt1 <> b.netw
 ;

这让我想起了这个:

 nt1 license      netw

   101 150275       102
   101 150275       108
   101 150275       113
   101 150275       114
   102 110438       101
   102 110438       108
   102 110438       113
   102 110438       114
   102 150275       101
   102 150275       108
   102 150275       113
   102 150275       114
   108 110438       101
   108 110438       102
   108 110438       113
   108 110438       114
   108 150275       101
   108 150275       102
   108 150275       113
   108 150275       114
   113 110438       101
   113 110438       102
   113 110438       108
   113 110438       114
   113 150275       101
   113 150275       102
   113 150275       108
   113 150275       114
   114 110438       101
   114 110438       102
   114 110438       108
   114 110438       113

最佳答案

对于 SQL Server,您可以使用此:

我使用虚拟数据创建表。

CREATE TABLE #x_pns_address (
    network int,
    license int
)

INSERT INTO #x_pns_address VALUES
(101,12345),
(102,12345),
(108,12345),
(101,54321),
(102,54321),
(108,54321),
(113,54321),
(114,54321),
(101,98765),
(108,98765)

如您所见,许可证 54321 拥有全部 5 个网络,而 1234598765 拥有 2 和 3 个网络。

然后我们运行:

INSERT INTO #x_pns_address
SELECT  p1.license,
        n.network
FROM (
    SELECT license --Here we get all licenses that missed any of the
    FROM #x_pns_address   --given numbers
    WHERE network IN (101,102,108,113,114)
    GROUP BY license
    HAVING COUNT(network) < 5
    ) as p1
CROSS JOIN ( --Cartesian join with license numbers we need
    SELECT * FROM (VALUES (101),(102),(108),(113),(114)) as t(network)
) as n
LEFT JOIN #x_pns_address x --and here we get only those that are not in main table
    ON x.network = n.network and x.license = p1.license
WHERE x.license  IS NULL

#x_pns_address 中,我们添加 5 行缺少网络的许可证 1234598765:

license network
12345   113
12345   114
98765   102
98765   113
98765   114

关于mysql - 我如何加入这些表并获得所需的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38119761/

相关文章:

sql - 如何在 SQL Server 2008 中将 varchar(4) 转换为 float?

mysql - 将十六进制数据从 varchar 类型字段移动到 bigint 类型 (mysql)

mysql - 存储过程 - 错误 #1064

php - 防止多次查询PDO

php - 如何从codeigniter事件记录中获取result_array列表到row_array中

mysql - R RMySQL 查询使日文字符变形

sql - 如何查询相关记录之间的Linkage?

sql-server - SQL Server 是否可以在同一个事务的同一行上应用 s 锁和 x 锁?

sql-server - 什么时候压缩 Sql Server 数据库?

c# - asp.net - C# 应用程序 - sql server 的正确参数化查询类