Rui*_*ito 11 mysql sql json node.js sequelize.js
为了返回以下JSON示例,我们需要查询10个表,同时查找其间的值.我对SQL的了解有限,所以我们在这里寻求帮助.
{
project: 1,
name: "BluePrint1",
description: "BluePrint 1 Description",
listWorkPackages: [
{
id: 1,
name: "WorkPackage 1 Name",
description: "WorkPackage 1 Description",
type: "WorkPackage Type",
department: "WorkPackage Department",
status: "Workpackage work status"
},
{
id: 2,
name: "WorkPackage 2 Name",
description: "WorkPackage 2 Description",
type: "WorkPackage Type",
department: "WorkPackage Department",
status: "Workpackage work status"
}
],
assignments: [
{
id: 3,
name: "WorkPackage 3 Name",
description: "WorkPackage 3 Description",
type: "WorkPackage Type",
department: "WorkPackage Department",
status: "Workpackage work status"
}
]
}
Run Code Online (Sandbox Code Playgroud)
数据库看起来像这样(在新标签中打开以获取更多详细信息):
使用WorkerID,我们希望所有WorkPackages:
所以我们可以发送JSON上的信息,我们需要查看这10个表:
我对SQL的了解仅限于JOIN:
SELECT *
FROM BL_Blueprint
JOIN PR_Project ON BL_idBlueprint = PR_idBlueprint
JOIN WP_WorkPackage ON BL_idBlueprint = WP_idBlueprint
JOIN WE_WorkPackageExecution ON WE_idWorkPackage = WP_idWorkPackage
JOIN DP_Department ON WE_idDepartment = DP_idDepartment
Run Code Online (Sandbox Code Playgroud)
我们只需要搜索与Worker 类型相同的工作包,但只有在查看表WT_WorkerType之后我们才知道这些类型.
我读到了关于你可以在WHERE字段中选择SELECT的subQuery,但是无法理解它,并得到一个有效的查询.
最后,我的问题是:
我正在使用Sequelize,如果它可以提供帮助,但是从文档中我认为Raw Query会更容易.
谢谢大家的帮助和支持.
所有的爱都归于@MihaiOvidiuDrăgoi(在评论中),帮助我找到了解决方案
第一个 SELECT获取赋值,第二个获取上面描述的逻辑.标签有助于确定哪个是哪个,我们为了简化JSON的创建而命令.
SELECT *
FROM
((SELECT
BL_Name,
BL_Description,
WP_Name,
WP_Description,
PR_idProject,
WE_idWorkPackageExecution,
WE_idWorkStatus,
TY_TypeName,
TY_Description,
WS_WorkStatus,
DP_Name,
DP_Description,
'second_select'
FROM
WK_Worker, WP_WorkPackage
INNER JOIN BL_Blueprint ON BL_idBlueprint = WP_idBlueprint
INNER JOIN PR_Project ON PR_idBlueprint = BL_idBlueprint
INNER JOIN WE_WorkPackageExecution ON WE_idWorkPackage = WP_idWorkPackage
AND WE_idProject = PR_idProject
INNER JOIN WS_WorkStatus ON WS_idWorkStatus = WE_idWorkStatus
INNER JOIN DP_Department ON DP_idDepartment = WE_idDepartment
INNER JOIN WA_WorkAssignments ON WA_idWorkPackageExecution = WE_idWorkPackageExecution
INNER JOIN TY_Type ON TY_idType = WP_idType
WHERE
WA_idWorker = 1 AND WK_idWorker = 1) UNION ALL (SELECT
BL_Name,
BL_Description,
WP_Name,
WP_Description,
PR_idProject,
WE_idWorkPackageExecution,
WE_idWorkStatus,
TY_TypeName,
TY_Description,
WS_WorkStatus,
DP_Name,
DP_Description,
'first_select'
FROM
WK_Worker, WP_WorkPackage
JOIN BL_Blueprint ON BL_idBlueprint = WP_idBlueprint
JOIN PR_Project ON PR_idBlueprint = BL_idBlueprint
JOIN WE_WorkPackageExecution ON WE_idWorkPackage = WP_idWorkPackage
AND WE_idProject = PR_idProject
JOIN WS_WorkStatus ON WS_idWorkStatus = WE_idWorkStatus
JOIN DP_Department ON DP_idDepartment = WE_idDepartment
JOIN TY_Type ON TY_idType = WP_idType
WHERE
WK_idWorker = 1
AND DP_idDepartment IN
(SELECT
WK_idDepartment
FROM
WK_Worker
WHERE
WK_idWorker = 1)
AND WP_idType IN
(SELECT
TY_idType
FROM
TY_Type
JOIN WT_WorkerType ON TY_idType = WT_idType
WHERE
WT_idWorker = 1)
)
) AS T1
ORDER BY T1.PR_idProject
Run Code Online (Sandbox Code Playgroud)
小智 2
由于您似乎需要一个由两个不同连接组成的单个结果集,因此您可能应该执行类似的操作
SELECT * from
(
SELECT 1
UNION ALL
SELECT 2
) a
ORDER by ...
Run Code Online (Sandbox Code Playgroud)
您的两个 select 语句还应包含逻辑名称作为不同的列(如“firstselect”) - 这样您就知道您的行来自哪个选择。