Suppose there are three tables: A , B and C . Each has id column ( INTEGER NOT NULL PRIMARY KEY ) and data column ( TEXT ). There is also another table - records - with fields:
record_id INTEGER NOT NULL PRIMARY KEY,
a_id INTEGER,
b_id INTEGER,
c_id INTEGER,
...
How to select A . data , B . data and C . data for a certain record ( record_id = <some value>
) by a_id , b_id and c_id accordingly when the latter can be either valid IDs or zeros (so each column in the result can be a TEXT value or NULL
)?
Assuming the Sample data, following is my query using left joins-
select r.record_id,
a.text as a_txt,
b.text as b_txt,
c.text as c_txt
from records r
left join tablea a
on r.a_id=a.id
left join tableb b
on r.b_id=b.id
left join tablec c
on r.c_id=c.id
where r.record_id=<Some Value>;
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.