如何从这些表中获取他们使用过的用户和计算机列表?

时间:2016-08-22 15:31:11

标签: sql sql-server

我的程序中发生了一个奇怪的异常现象。我有一个软件将用户和计算机连接在一起,然后将用户信息发送到云中的服务器。

将用户和计算机连接在一起的软件将用户/计算机信息存储在这些表结构中的本地数据库中:

CREATE TABLE [dbo].[Computers](
[ID] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Computers] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



CREATE TABLE [dbo].[User_Computer](
[ID] [uniqueidentifier] NOT NULL,
[ID_User] [uniqueidentifier] NOT NULL,
[ID_Computer] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_User_Computer] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[User_Computer]  WITH CHECK ADD  CONSTRAINT [FK_User_Computer_Computers] FOREIGN KEY([ID_Computer])
REFERENCES [dbo].[Computers] ([ID])
GO

ALTER TABLE [dbo].[User_Computer] CHECK CONSTRAINT [FK_User_Computer_Computers]
GO

ALTER TABLE [dbo].[User_Computer]  WITH CHECK ADD  CONSTRAINT [FK_User_Computer_Users] FOREIGN KEY([ID_User])
REFERENCES [dbo].[Users] ([ID])
GO

ALTER TABLE [dbo].[User_Computer] CHECK CONSTRAINT [FK_User_Computer_Users]
GO


CREATE TABLE [dbo].[Users](
[ID] [uniqueidentifier] NOT NULL,
[Name] [nchar](50) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

用户/计算机存储在本地数据库中后,用户guid用于将数据传输到云中的服务器。有趣的是云中的服务器有200个用户guids,当我运行这个脚本时,我的本地数据库只有177个。

SELECT
Users.ID AS ID_User, 
Users.Name AS UserName, 
User_Computer.ID AS Association, 
User_Computer.ID_Computer, 
Computers.Name AS ComputerName
FROM Users INNER JOIN User_Computer ON Users.ID = User_Computer.ID_User     INNER JOIN
Computers ON User_Computer.ID_Computer = Computers.ID

我的问题:无论如何我的脚本没有正确收集所有用户信息吗?

注意:我正在正确地将数据放入用户和计算机表中。我感兴趣的是脚本是否正在使用正确的连接。它应该是内在的,外在的还是完全不同的东西?

注意:(再次)当我在本地计算机上运行时,我得到了这个:

5FCD88C8-04B5-494C-88C8-85BCD08CBBB5    Fred                                            B945300D-7CED-42FC-8A79-4FDBB54F6B69    29CAD425-42A0-478F-8966-1448144EB90E    Comp1
E357B7E7-7328-4D2D-9A3E-20FC388C5781    Joe                                             7BB73859-8CE3-4383-BAFF-504DBF719524    182627C5-F91D-4C88-9AE3-A527E55A5A41    Comp2
F8C2DB79-85AC-408A-A858-B0C0FA6862F7    Moe                                                 23267708-2A5E-497A-B91A-937D28983832    B518614E-A243-47D1-B642-D24B434D7683    Comp3
237947BC-C26D-44D0-9AF7-F231D98F1BF3    Curly                                               F89411A9-787B-4A2A-AA1E-B56455B781E8    29CAD425-42A0-478F-8966-1448144EB90E    Comp4
3C8DCE89-6764-4D57-B0AD-2CF988EADB35    Steve                                               1D464AB1-DA70-4ACC-8D00-ED7F47D35413    9446327A-30BA-492F-9A28-3AB132C31988    Comp5
F8C2DB79-85AC-408A-A858-B0C0FA6862F7    Moe                                                 A32FD03E-B777-4D74-8702-F58D71B53E8B    82A39A46-269B-43D5-B7A6-B14A9D5FBBD4    Comp6

所以...我真正想知道的存储过程是所有用户的列表以及他们与之关联的计算机。

我想要做的是将这两个存储过程合并为一个:

SELECT
    Users.ID AS ID_User, 
    Users.Name AS UserName, 
    User_Computer.ID AS Association, 
    User_Computer.ID_Computer
FROM Users INNER JOIN User_Computer ON Users.ID = User_Computer.ID_User 

SELECT Computers.ID, 
   Computers.Name, 
   User_Computer.ID AS Association, 
   User_Computer.ID_User
FROM   Computers INNER JOIN User_Computer ON Computers.ID = User_Computer.ID_Computer

1 个答案:

答案 0 :(得分:1)

此查询:

SELECT u.ID AS ID_User, u.Name AS UserName, 
       uc.ID AS Association, uc.ID_Computer, 
       c.Name AS ComputerName
FROM Users u INNER JOIN
     User_Computer uc
     ON u.ID = uc.ID_User INNER JOIN
     Computers c
     ON uc.ID_Computer = c.ID;

为数据中的每个用户/计算机对返回一行。如果设置了外键,则其行数应与User_Computer中的行数相同。

如果某些用户没有计算机(评论无法说明),那么您可以使用LEFT JOIN来获取所有用户以及相关计算机的相关信息(如果有) / p>

SELECT u.ID AS ID_User, u.Name AS UserName, 
       uc.ID AS Association, uc.ID_Computer, 
       c.Name AS ComputerName
FROM Users u LEFT JOIN
     User_Computer uc
     ON u.ID = uc.ID_User LEFT JOIN
     Computers c
     ON uc.ID_Computer = c.ID;

如果您的查询和其他存储过程返回不同的结果,则(1)它们在不同的表(可能在不同的数据库中)上运行,或者(2)它们实现不同的逻辑。没有看到其他逻辑,就不可能说出差异是什么。