MySQL 查询 10 个表(Sequelize 或 Raw Query)

Posted

技术标签:

【中文标题】MySQL 查询 10 个表(Sequelize 或 Raw Query)【英文标题】:MySQL Query 10 Tables (Sequelize or Raw Query) 【发布时间】:2016-01-03 01:52:59 【问题描述】:

为了返回以下 JSON 示例,我们需要查询 10 个表,同时查找其间的值。 我对 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

具有与 Worker 相同的 Type; 并且属于同一个部门; 还有直接Assignment通过表 WA_Assignments

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

    WK_Worker WT_WorkerType TY_Type WP_WorkPackage WE_WorkPackageExecution WS_WorkStatus BL_Blueprint PR_Project DP_部门 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的工作包,但我们事先不知道类型,只有在查看表WT_WorkerType

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

问题:

最后,我的问题是:

    SQL 查询;

我正在使用Sequelize,如果它可以提供帮助,但从文档中我认为原始查询会更容易。

感谢大家的帮助和支持。


解决方案

所有的爱都归功于@MihaiOvidiuDrăgoi(在 cmets 中)帮助我找到了解决方案

First SELECT 获取分配,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

【问题讨论】:

你能把那个不工作的地方贴出来吗?这可能有助于理解您尝试实现的逻辑。 @MihaiOvidiuDrăgoi 当然。从网上我到了。现在我正在尝试查看是否可以从其他表中获取信息。我必须小心加入。我走上正轨了吗? 尝试只加入您需要数据的表(或链接这些表)。之后我们将在 WHERE 上工作:) @MihaiOvidiuDrăgoi 抱歉,我已经有 2 个在哪里了。我认为所有的 JOIN 都在那里,作业应该在 Where 上还是作为 JOIN? 您能否发布当前查询的结果与预期结果? 【参考方案1】:

由于您似乎需要由两个不同连接组成的单个结果集,因此您可能应该执行类似的操作

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

您的两个 select 语句还应将逻辑名称包含为不同的列(例如“firstselect”) - 这样您就可以知道您的行来自哪个选择。

【讨论】:

【参考方案2】:

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

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

);

【讨论】:

我最终将您的回复用于我遇到的另一个案例。感谢您的帮助。

以上是关于MySQL 查询 10 个表(Sequelize 或 Raw Query)的主要内容,如果未能解决你的问题,请参考以下文章

在 sequelize 中检查 mysql 连接

mySQL/Sequelize - 如何查询以获取字段为空数组的数据

Sequelize 关联和查询此 mysql 查询

如何在 MySql 中使用 sequelize 运行多个原始查询?

当 GraphQL 查询只需要返回对象的某些字段时,为啥 MySQL/Sequelize 会执行全选查询?

使用 Sequelize 在 GraphQL 查询中将日期时间字段输出为字符串