select data from 2 tables using a third as data glue - sqlite

I need any help getting data from three tables.
This is my setup:
TableA with two columns: id, name
TableB with three columns: id_a, id_c1, id_c2
TableC with two columns: id, name
I want as result the following table:
TableA.name,TableC.name,TableC.name
where the TableC.name(s) are expansion of id_c1, id_c2 of TableB.
Any idea?
Very thanks

You need to join the 3 tables like this:
select a.name, c1.name, c2.name
from tablea a inner join tableb b
on b.id_a = a.id
inner join tablec c1
on c1.id = b.id_c1
inner join tablec c2
on c2.id = b.id_c2
Maybe instead of inner joins you need left joins, but I can't tell for sure.

Related

inner join on column A then on column B if A is empty then column C if B is empty

I have a large query which combines a lot of tables. I don't want to make this question to complicate so I will try to ask it easy:
suppose I have 3 columns:
table1_device_id, table2_device_id, table3_device_id
I know want to achieve this:
Do a left join on table1_device_id, if that column is null, do a left join on table2_device_id,.. (same for table 3..)
I have following example for 1 table now:
left join query_505727 as inside_weather_data on y.datetime_weather = inside_weather_data.time AND (a.monitor_id is null or a.monitor_id = inside_weather_data.device_id)
but how to do that for multiple?
my query:
select y.datetime_weather, a.site_name as site_name,a.name as house_name, a.name1 as floor_name,a.airspace_name as room_name, a.registration,a.monitor_id, device_color_table.device_id as device_color_device_id, inside_weather_data.device_id as weather_device_id, rdi_data.device_id as rdi_device_id, a.monitor_code as monitor_code, a.alarm_type,a.created_at_date as alarm_date,device_color_table.colors as LED_color_status, rdi_data.value as RDI, ROUND(inside_weather_data.min_inside_temp, 2) as min_inside_temp, ROUND(inside_weather_data.max_inside_temp, 2) as max_inside_temp, y.max_temp as max_outside_temp, y.min_temp as min_outside_temp, y.average_temp as avg_temp, a.alarm_type, a.priority, a.translation_code,a.created_at
from query_505641 as y
left join query_505127 as a on a.created_at_date = y.datetime_weather
left join query_505241 as device_color_table on device_color_table.datetime = y.datetime_weather AND device_color_table.device_id = a.monitor_id
left join query_506556 as rdi_data on rdi_data.time = y.datetime_weather and rdi_data.device_id = a.monitor_id AND device_color_table.device_id is null
left join query_505727 as inside_weather_data on y.datetime_weather = inside_weather_data.time AND inside_weather_data.device_id = a.monitor_id AND coalesce(device_color_table.device_id, rdi_data.device_id) is null
If the table that you want to join to these 3 tables is table then use these LEFT joins:
....
from table t
left join table1 t1 on t1.table1_device_id = t.device_id
left join table2 t2 on t2.table2_device_id = t.device_id and t1.table1_device_id is null
left join table3 t3 on t3.table3_device_id = t.device_id and coalesce(t1.table1_device_id, t2.table2_device_id) is null

sql query from multiple random tables

i have 3 tables
table 1( one id to one name)
ID|name
1| john
2|mike
3|olga
4|juliet
table 2 ()
ID|adress
1|xxx
1|yyy
2|xx
2|z
3|xxx
3|yy
table 3 ()
address|buildings
xxx|flat
xxx|building
z|flat
z|building
z|park
z|lot
yy|building
yy|park
i wish to find the name of those wanting to stay at both building and flat, all values are random so i can only search with buildings, i have already managed to query for only one type of buildings but i am missing something as to validate the needed AND
You have to join all 3 tables and group by name:
SELECT t1.name FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table3 t3 ON t3.address = t2.address
WHERE t3.buildings IN ('building', 'flat')
GROUP BY t1.name
HAVING COUNT(DISTINCT t3.buildings) = 2

PL SQL nested subquery

I'm trying to get the result into a variable (is it possible to do it as a %rowtype of an existing table? probably not because of conflicting columns) where it would display me all the values where the two refs overlap and the i_ref(which is inputted ) also overlaps with the ref from t1.
select *
into aRow
from table1 t1
where t1.ref = i_ref
and (select * from table2 t2 where t1.ref = t2.ref);
What am I doing wrong with my select?
You can join tables instead of nested subquery:
select t1.*
into aRow
from table1 t1 join table2 t2 on t1.ref = t2.ref
where t1.ref = i_ref

SQL column ambiguously defined

I have a problem in this code:
select idingrediente
into niding
from ingredientes
inner join RECHASING on ingredientes.IDINGREDIENTE = RECHASING.IDINGREDIENTE
inner join receta on RECHASING.RIDRECETA = receta.IDRECETA;
Error:
Column ambiguously defined
Table ingredientes:
idingrediente
ingrediente
cantingrediente
Table rechasing:
idingrediente
idreceta
Table receta:
idReceta
Cantingrediente
rechasing is a intermediate table because receta has n:n ingredientes.
Thanks
Just give aliases to your tables, then identify the idingrediente column with an alias:
SELECT t1.idingrediente -- or t2.idingrediente
INTO niding
FROM ingredientes t1
INNER JOIN rechasing t2
ON t1.idingrediente = t2.idingrediente
INNER JOIN receta t3
ON t2.ridreceta = t3.idreceta

SQLite - LEFT JOIN

I need to make a query like this:
SELECT table1.*, table2.column
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
But it's not working. If I try the same query but replacing the first part -- >
SELECT table1.column, table2.column
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
it works fine. I need to bring all the columns from table1. How can I make it without specifing all of them?
If you have the same column name in table1 and table2, here is the solution for not specifying all the column name of table1 :
SELECT table1.*, table2.column as my_column_name_no_in_table1
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
If the column names of table1 and table2 are all different, you can use :
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
But as said in the peterm's comment, it is not a good practice in production. Now, do as you want! ;)

Resources