我有以下 3 个表格和示例数据:
员工、地址、州
/****** Object: Table [AzureSearch].[Address] Script Date: 11/2/2021 2:16:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AzureSearch].[Address](
[EmployeeId] [int] NULL,
[Address] [nvarchar](60) NULL,
[City] [nvarchar](30) NULL
) ON [PRIMARY]
GO
/****** Object: Table [AzureSearch].[Employee] Script Date: 11/2/2021 2:16:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AzureSearch].[Employee](
[EmployeeId] [int] NOT NULL,
[FirstName] [nvarchar](200) NOT NULL,
[LastName] [nvarchar](200) NOT NULL
) ON [PRIMARY]
GO
/****** Object: Table [AzureSearch].[States] Script Date: 11/2/2021 2:16:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AzureSearch].[States](
[City] [varchar](50) NULL,
[StateName] [varchar](50) NULL
) ON [PRIMARY]
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (1, N'4350 Minute Dr.', N'Newport Hills')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (2, N'7559 Worth Ct.', N'Renton')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (3, N'2137 Birchwood Dr', N'Redmond')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (4, N'5678 Lakeview Blvd.', N'Minneapolis')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (5, N'9435 Breck Court', N'Bellevue')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (6, N'5670 Bel Air Dr.', N'Renton')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (7, N'1873 Lion Circle', N'Bothell')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (7, N'3858 Vista Diablo', N'Issaquah')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (8, N'4912 La Vuelta', N'Bothell')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (8, N'4039 Elkwood Dr.', N'Ballard')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (1, N'Ken', N'Sánchez')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (2, N'Terri', N'Duffy')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (3, N'Roberto', N'Tamburello')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (4, N'Rob', N'Walters')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (5, N'Gail', N'Erickson')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (6, N'Jossef', N'Goldberg')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (7, N'Osarumwense', N'Agbonile')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (8, N'Karl', N'Xie')
GO
INSERT [AzureSearch].[States] ([City], [StateName]) VALUES (N'Bothell', N'Washington')
GO
INSERT [AzureSearch].[States] ([City], [StateName]) VALUES (N'Ballard', N'Utah')
GO
当我运行以下查询时,我在一个 JSON 对象中获取 JSON 详细信息
SELECT e.EmployeeId
,(
SELECT a.Address
,a.City
-- ,s.StateName AS StateName
FROM AzureSearch.[Address] AS a
-- ,AzureSearch.States AS s
WHERE a.EmployeeId = e.employeeId
-- AND a.City = s.City
FOR json auto
) AS AddressDetails
FROM azuresearch.Employee AS e
WHERE e.EmployeeId = 8
AddressDetails 列中的结果:
[
{
"Address": "4912 La Vuelta",
"City": "Bothell"
},
{
"Address": "4039 Elkwood Dr.",
"City": "Ballard"
}
]
但是,如果您取消注释上述 SQL 查询中的注释行以包含 States 表中的列,我会得到如下结果:
为什么StateName
细节进入数组?
我可以通过调整查询将其与地址和城市详细信息一起放在外面吗?