0

我需要使用SQL Server Management Studio 查询设计器构建我的查询

tbl_B.Name + tbl_C.Name + tbl_D.Name如果Idintbl_E/tbl_User等于UserIdin ,我需要显示一个串联的字符串tbl_A

这是结构 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)
4

1 回答 1

1

下面的查询将为您提供每个 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"
于 2020-10-23T03:27:30.390 回答