如何显示来自 4 个不同表的数据 - 查询设计器
How to display data from 4 different tables - Query Designer
我需要使用 SQL Server Management Studio Query Designer
构建我的查询
我需要显示 tbl_B.Name + tbl_C.Name + tbl_D.Name
的串联字符串,如果 tbl_E/tbl_User
中的 Id
等于 tbl_A
中的 UserId
这是Table结构https://imgur.com/PZagLPq, https://imgur.com/moE4dcf
tbl_A UserId = Id in tbl_E/tbl_User
tbl_B_Id = Id in tbl_B
tbl_C_Id = Id in tbl_C
tbl_D_Id = Id in tbl_D
我尝试了下面的代码,但没有显示来自 tbl_B、tbl_C 和 tbl_D
的名称字段
SELECT tbl_B.Name AS Expr1, tbl_C.Name AS Expr2, tbl_D.Name AS Expr3, tbl_A.UserId, [tbl_E].Id AS Expr5
FROM tbl_B
INNER JOIN tbl_D ON tbl_B.Id = tbl_D.Id
INNER JOIN tbl_A ON tbl_B.Id = tbl_A.Id
INNER JOIN tbl_C ON tbl_B.tbl_CId = tbl_C.Id AND tbl_D.Id = tbl_C.tbl_DId
INNER JOIN [tbl_E] ON tbl_B.Id = [tbl_E].Id AND tbl_A.UserId = [tbl_E].Id
WHERE (tbl_A.UserId = [tbl_E].Id)
下面的查询将为您提供每个 userId 的串联名称字符串,它在所有表中都有条目。
SELECT
"tbl_A"."UserId",
CONCAT("tbl_B"."Name", '-', "tbl_C"."Name", '-', "tbl_D"."Name"),
"tbl_B"."Name" AS Expr1,
"tbl_C"."Name" AS Expr2,
"tbl_D"."Name" AS Expr3
FROM "tbl_E"
INNER JOIN "tbl_A" ON "tbl_A"."UserId" = "tbl_E"."id"
INNER JOIN "tbl_B" ON "tbl_B"."id" = "tbl_A"."tbl_B_id"
INNER JOIN "tbl_C" ON "tbl_C"."id" = "tbl_B"."tbl_C_id"
INNER JOIN "tbl_D" ON "tbl_D"."id" = "tbl_C"."tbl_D_id"
我需要使用 SQL Server Management Studio Query Designer
构建我的查询我需要显示 tbl_B.Name + tbl_C.Name + tbl_D.Name
的串联字符串,如果 tbl_E/tbl_User
中的 Id
等于 tbl_A
UserId
这是Table结构https://imgur.com/PZagLPq, https://imgur.com/moE4dcf
tbl_A UserId = Id in tbl_E/tbl_User
tbl_B_Id = Id in tbl_B
tbl_C_Id = Id in tbl_C
tbl_D_Id = Id in tbl_D
我尝试了下面的代码,但没有显示来自 tbl_B、tbl_C 和 tbl_D
的名称字段SELECT tbl_B.Name AS Expr1, tbl_C.Name AS Expr2, tbl_D.Name AS Expr3, tbl_A.UserId, [tbl_E].Id AS Expr5
FROM tbl_B
INNER JOIN tbl_D ON tbl_B.Id = tbl_D.Id
INNER JOIN tbl_A ON tbl_B.Id = tbl_A.Id
INNER JOIN tbl_C ON tbl_B.tbl_CId = tbl_C.Id AND tbl_D.Id = tbl_C.tbl_DId
INNER JOIN [tbl_E] ON tbl_B.Id = [tbl_E].Id AND tbl_A.UserId = [tbl_E].Id
WHERE (tbl_A.UserId = [tbl_E].Id)
下面的查询将为您提供每个 userId 的串联名称字符串,它在所有表中都有条目。
SELECT
"tbl_A"."UserId",
CONCAT("tbl_B"."Name", '-', "tbl_C"."Name", '-', "tbl_D"."Name"),
"tbl_B"."Name" AS Expr1,
"tbl_C"."Name" AS Expr2,
"tbl_D"."Name" AS Expr3
FROM "tbl_E"
INNER JOIN "tbl_A" ON "tbl_A"."UserId" = "tbl_E"."id"
INNER JOIN "tbl_B" ON "tbl_B"."id" = "tbl_A"."tbl_B_id"
INNER JOIN "tbl_C" ON "tbl_C"."id" = "tbl_B"."tbl_C_id"
INNER JOIN "tbl_D" ON "tbl_D"."id" = "tbl_C"."tbl_D_id"