I got 5 tables with certain values:
tbl_1
+-------+-------+
| nm_id | name |
+-------+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
+-------+-------+
tbl_2
+-------+-------+-------+
|post_id| nm_id | post |
+-------+-------+-------+
| 1 | 1 | text1 |
| 2 | 1 | text2 |
| 3 | 2 | text3 |
+-------+-------+-------+
tbl_3
+-------+-------+-------+-------+
|com_id |post_id| nm_id | com |
+-------+-------+-------+-------+
| 1 | 1 | 1 | text1 |
| 2 | 2 | 1 | text2 |
| 3 | 2 | 2 | text3 |
+-------+-------+-------+-------+
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
+-------+-------+-------+-------+-------+-------+
|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. So far what I do is:
tbl_2
then join tbl_1
on nm_id
then exclude tbl_5
on 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
-> 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
-> 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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.