假设有三个表:A,B和C.每个表都有id列(INTEGER NOT NULL PRIMARY KEY)和数据列(TEXT)。还有另一个表 - 记录 - 包含字段:
record_id INTEGER NOT NULL PRIMARY KEY,
a_id INTEGER,
b_id INTEGER,
c_id INTEGER,
...
如果后者可以是有效ID或零,那么如何通过a_id,b_id和c_id为某个记录(record_id = <some value>
)选择A.data,B.data和C.data(因此结果中的每一列都可以是TEXT值)还是NULL
)?
假设Sample数据,以下是我使用左连接的查询 -
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>;