【问题标题】:Connecting 5 tables but excluding values from the 5th连接 5 个表,但不包括第 5 个表的值
【发布时间】:2020-03-29 11:18:17
【问题描述】:

数据表

我有 5 个具有特定值的表:

  1. 表:tbl_1
+-------+-------+
| nm_id | name  |
+-------+-------+
|     1 | name1 |
|     2 | name2 |
|     3 | name3 |
+-------+-------+
  1. 表:tbl_2
+-------+-------+-------+
|post_id| nm_id | post  |
+-------+-------+-------+
|     1 |     1 | text1 |
|     2 |     1 | text2 |
|     3 |     2 | text3 |
+-------+-------+-------+
  1. 表:tbl_3
+-------+-------+-------+-------+
|com_id |post_id| nm_id | com   |
+-------+-------+-------+-------+
|     1 |     1 |     1 | text1 |
|     2 |     2 |     1 | text2 |
|     3 |     2 |     2 | text3 |
+-------+-------+-------+-------+
  1. 表:tbl_4
+-------+-------+-------+-------+-------+
|com2_id|com_id||post_id| nm_id | com2  |
+-------+-------+-------+-------+-------+
|     1 |     1 |     1 |     1 | text1 |
|     2 |     2 |     2 |     1 | text2 |
|     3 |     1 |     2 |     2 | text3 |
+-------+-------+-------+-------+-------+
  1. 表:tbl_5
+-------+-------+-------+-------+-------+-------+
|rep_id |com2_id|com_id||post_id| nm_id | text  |
+-------+-------+-------+-------+-------+-------+
|     1 |  null |     1 |     1 |     1 | text1 |
|     2 |  null |  null |     1 |     1 | text2 |
+-------+-------+-------+-------+-------+-------+

我尝试了什么

我需要获取所有值,但我的想法是从结果中排除 tbl_5 中的值。到目前为止,我所做的是:

  1. 选择tbl_2 然后在nm_id 上加入tbl_1 然后在post_id 上排除tbl_5 -> 1stVariable
SELECT tbl_2.*, name.tbl_1
FROM tbl_2
INNER JOIN tbl_2 ON tbl_1.nm_id = tbl_2.nm_id
LEFT JOIN tbl_5 ON tbl_2.post_id = tbl_5.post_id
WHERE tbl_2.post_id = *variable* AND tbl_5.rep_id IS NULL
  1. 选择tbl_3 然后在com_id 上加入tbl_2 然后在nm_id 上加入tbl_1com_id 上排除tbl_5 -> 2ndVariable
SELECT tbl_3.*, name.tbl_1
FROM tbl_3
INNER JOIN tbl_3 ON tbl_1.nm_id = tbl_3.nm_id
LEFT JOIN tbl_5 ON tbl_3.com_id = tbl_5.com_id
WHERE tbl_3.com_id = *variable* AND tbl_5.rep_id IS NULL
  1. 选择tbl_4 然后在nm_id 上加入tbl_1com2_id 上排除tbl_5 -> 3rdVariable
SELECT tbl_4.*, name.tbl_1
FROM tbl_4
INNER JOIN tbl_4 ON tbl_1.nm_id = tbl_4.nm_id
LEFT JOIN tbl_5 ON tbl_4.com2_id = tbl_5.com2_id
WHERE tbl_4.com2_id = *variable* AND tbl_5.rep_id IS NULL

我当前的输出是 JSON

{
  post:{...},
  com:{...},
  com2:{...}
}

需要的 JSON 输出

发生的情况是我的 .js 变得如此长且复杂,包含 3 个单独的数组。我试图在第一个变量中获取所有内容,但我不明白如何在数组中获取数组:

{
   post: {
     post_id: "post_id",
     name: "name",
     com: {
       com_id:"com_id",
       com:"com",
       name:"name",
       com2:{
         com2_id:"com2_id",
         com2:"com2",
         name:"name",
         }
      },
   }
}

我真的希望这是可以理解的。提前致谢。

【问题讨论】:

  • 请将数据显示为文本而不是图像
  • 我很抱歉,但这是我知道如何正确显示的唯一方法。今天刚加入...我会尝试编辑。
  • 您应该花时间正确格式化您的帖子。这会带来更好的答案机会。我在您的帖子中添加了一些格式示例。将表格格式化为 ASCII-table,以便您可以将它们嵌入为文本,例如:ozh.github.io/ascii-tables
  • @hc_dev 我希望这次格式正确,感谢您在这方面的帮助!
  • @Djisin 格式正确。大多数 SO 成员需要通过将代码发布为 minimal reproducible example 来显示您的尝试,因此您可以 将您的 SQL 添加到预格式化的代码块(否则删除我的 SELECT 示例,因为它不反映您的情况)。

标签: mysql sql arrays json


【解决方案1】:

我希望这样的查询:

select . . .    -- columns you want
from . . .      -- joins among the first four tables
where not exists (select 1
                  from table_5 t5
                  where . . .    -- conditions that match table 5 to the other tables
                 );

我可以推测你的条件是这样的:

where not exists (select 1
                  from table_5 t5
                  where t5.com_id = t4.com_id and
                        t5.post_id = t2.post_id and
                        t5.nm_id = t1.nm_id
                 )

但您的问题不清楚表格是如何匹配的。

【讨论】:

  • 我再次更新了问题,希望现在更清楚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
相关资源
最近更新 更多