Youness El Azzaoui
Youness El Azzaoui

Reputation: 25

how to find tables that have references to a primary key of the parent table in SQLite

Let say we have 1 parent table (clients table) and many child tables (form table) in SQLite.

Each of the child tables have a foreign key (client_id) referencing the primary key of the parent table (client_id)

I have the name and the id of one client, (say client1), my goal is to find the forms that belong to that specific client, which means to find the tables that have foreign key referencing client_id: 1,

In the example bellow we have 1 parent element and 3 child tables, 2 of which reference client1 in the clients table, so I want to be able to select those 2 tables based on the primary key of the parent table (client_id) using SQLITE

Note: I do not have the knowledge of names of the child tables, I want to select the specific tables solely based on the client_id and name

Clients Table

client_id name
1 client1
2 client2

Form Table1 (example of one of the many child tables)

id client_Id data
1 1 data1...
2 1 data2...

form Table2 (example of one of the many child tables)

data_id client _Id Group
1 2 dataA...
2 2 dataB...

form Table3 (example of one of the many child tables)

data_id client _Id Group
1 1 dataA...
2 1 dataB...

Upvotes: 0

Views: 219

Answers (1)

Zahidli
Zahidli

Reputation: 59

you can use inner join.

SELECT a1, a2, b1, b2
FROM A
INNER JOIN B on B.f = A.f;

Upvotes: 1

Related Questions