[英]Connecting 5 tables but excluding values from the 5th
I got 5 tables with certain values:我得到了 5 个具有特定值的表:
tbl_1
表: tbl_1
+-------+-------+
| nm_id | name |
+-------+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
+-------+-------+
tbl_2
表: tbl_2
+-------+-------+-------+
|post_id| nm_id | post |
+-------+-------+-------+
| 1 | 1 | text1 |
| 2 | 1 | text2 |
| 3 | 2 | text3 |
+-------+-------+-------+
tbl_3
表: tbl_3
+-------+-------+-------+-------+
|com_id |post_id| nm_id | com |
+-------+-------+-------+-------+
| 1 | 1 | 1 | text1 |
| 2 | 2 | 1 | text2 |
| 3 | 2 | 2 | text3 |
+-------+-------+-------+-------+
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 |
+-------+-------+-------+-------+-------+
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 |
+-------+-------+-------+-------+-------+-------+
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:到目前为止,我所做的是:
tbl_2
then join tbl_1
on nm_id
then exclude tbl_5
on post_id
-> 1stVariable选择tbl_2
再加入tbl_1
上nm_id
然后排除tbl_5
上post_id
- > 1stVariableSELECT 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
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_2
上com_id
再加入tbl_1
上nm_id
排除tbl_5
上com_id
- > 2ndVariableSELECT 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
tbl_4
then join tbl_1
on nm_id
exclude tbl_5
on com2_id
-> 3rdVariable选择tbl_4
然后在tbl_5
上加入tbl_1
在nm_id
排除com2_id
-> 3rdVariableSELECT 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
{
post:{...},
com:{...},
com2:{...}
}
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.