AlexDarkVoid
AlexDarkVoid

Reputation: 542

SQLite3: select columns from multiple tables by many IDs

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)?

Upvotes: 0

Views: 144

Answers (1)

Rhythm
Rhythm

Reputation: 682

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>;

Upvotes: 1

Related Questions