简体   繁体   English

连接 5 个表,但不包括第 5 个表中的值

[英]Connecting 5 tables but excluding values from the 5th

Data tables数据表

I got 5 tables with certain values:我得到了 5 个具有特定值的表:

  1. Table: tbl_1表: tbl_1
+-------+-------+
| nm_id | name  |
+-------+-------+
|     1 | name1 |
|     2 | name2 |
|     3 | name3 |
+-------+-------+
  1. Table: tbl_2表: tbl_2
+-------+-------+-------+
|post_id| nm_id | post  |
+-------+-------+-------+
|     1 |     1 | text1 |
|     2 |     1 | text2 |
|     3 |     2 | text3 |
+-------+-------+-------+
  1. Table: tbl_3表: tbl_3
+-------+-------+-------+-------+
|com_id |post_id| nm_id | com   |
+-------+-------+-------+-------+
|     1 |     1 |     1 | text1 |
|     2 |     2 |     1 | text2 |
|     3 |     2 |     2 | text3 |
+-------+-------+-------+-------+
  1. Table: tbl_4表: 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. Table: tbl_5表: 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 |
+-------+-------+-------+-------+-------+-------+

What I tried我试过的

I need to get all the values, but the idea is to exclude values from tbl_5 from result.我需要获取所有值,但想法是从结果中排除tbl_5 So far what I do is:到目前为止,我所做的是:

  1. select tbl_2 then join tbl_1 on nm_id then exclude tbl_5 on post_id -> 1stVariable选择tbl_2再加入tbl_1nm_id然后排除tbl_5post_id - > 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. select tbl_3 then join tbl_2 on com_id then join tbl_1 on nm_id exclude tbl_5 on com_id -> 2ndVariable选择tbl_3再加入tbl_2com_id再加入tbl_1nm_id排除tbl_5com_id - > 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. select tbl_4 then join tbl_1 on nm_id exclude tbl_5 on com2_id -> 3rdVariable选择tbl_4然后在tbl_5上加入tbl_1nm_id排除com2_id -> 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

My current output is JSON我当前的输出是 JSON

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

Needed JSON output需要的 JSON 输出

What happens is that my .js gets so long and complicated with 3 separated arrays.发生的情况是我的 .js 变得如此冗长和复杂,有 3 个分离的数组。 I was trying to get everything within 1st variable, what i can't understand is how to get array within array:我试图在第一个变量中获取所有内容,我无法理解的是如何在数组中获取数组:

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

I really hope this is understandable.我真的希望这是可以理解的。 Thanks in advance.提前致谢。

I would expect a query like this:我希望这样的查询:

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
                 );

I can speculate that your conditions are something like:我可以推测你的条件是这样的:

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
                 )

but your question is not clear on how the tables are matched.但是你的问题不清楚表格是如何匹配的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM