簡體   English   中英

連接 5 個表,但不包括第 5 個表中的值

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

數據表

我得到了 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再加入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. 選擇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. 選擇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

我當前的輸出是 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",
         }
      },
   }
}

我真的希望這是可以理解的。 提前致謝。

我希望這樣的查詢:

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
                 )

但是你的問題不清楚表格是如何匹配的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM