MySQL 查询 10 个表(Sequelize 或原始查询)

MySQL Query 10 Tables (Sequelize or Raw Query)

为了 return 下面的 JSON 示例,我们需要查询 10 tables,同时查找其间的值。 我对SQL的了解有限,所以我们在这里寻求帮助。

JSON:

{
  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"
    }
  ]
}

数据库:

数据库如下所示(在新选项卡中打开以获取更多详细信息) :

逻辑:

使用 WorkerID,我们需要所有 WorkPackages

所以我们可以发送 JSON 上的信息,我们需要查看这 10 个 table:

  1. WK_Worker
  2. WT_WorkerType
  3. TY_Type
  4. WP_WorkPackage
  5. WE_WorkPackageExecution
  6. WS_WorkStatus
  7. BL_Blueprint
  8. PR_Project
  9. DP_Department
  10. WA_WorkAssignments

我的问题:

我对 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

并且我们只需要搜索与 Worker 具有相同 Type 的工作包,但我们不知道类型之前,只有在查看 table WT_WorkerType.

之后

我阅读了有关子查询的信息,您可以在其中 SELECT 在 WHERE 字段中,但无法理解它,并获得一个有效的查询。

问题:

最后,我的问题是:

  1. SQL查询;

我正在使用 Sequelize,如果它有帮助的话,但从文档来看我认为原始查询会更容易。

感谢大家的帮助和支持。


解决方案

感谢@MihaiOvidiuDrăgoi(在评论中)帮助我找到了解决方案

FirstSELECT获取赋值和Second逻辑如上所述。标签有助于识别哪个是哪个,我们为了简化 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

由于您似乎需要一个由两个不同连接组成的结果集,您可能应该做类似

的事情
SELECT * from
(
SELECT 1 
UNION ALL 
SELECT 2 
) a
ORDER by ...

您的 select 语句还应包含逻辑名称作为不同的列(如 'firstselect')- 这样您就会知道您的行来自哪个 select。

如果您想尝试使用 sequelize 进行子查询,您可以在查询的一部分中使用文字:

User.findAll({
  attributes: [
    [sequelize.literal('SELECT ...'), 'subq'],
  ]
}).then(function(users) {

});