SQL присоединяется к объединенной таблице - PullRequest
0 голосов
/ 04 марта 2019

table1

out_path_id
in_path_id
other_fields

table2

id
name
location_to_id
location_from_id
car_id
other_fields

местоположение

id
name

автомобиль

id
name

table1 out_path_id и in_path_id относится к table2 * id.

table2location_to_id и location_from_id относятся к location '* id.

table2' car_id относятся к car '* id.

Можно присоединиться к table1 и table2.

select t2out.name out_name, t2in.name in_name, t1.other_fields
from table1 t1
join table2 t2out on t2out.id=t1.out_path_id
join table2 t2in on t2in.id=t1.in_path_id 

Теперь я пытаюсь продолжить объединение с location и car.Что я могу сделать?

Ответы [ 2 ]

0 голосов
/ 04 марта 2019

Вы продолжаете использовать псевдонимы, как у вас.Вам понадобятся 4 псевдонима таблицы местоположений, так как у вас есть два местоположения в таблице2.Вам понадобятся два псевдонима таблицы автомобилей, поскольку в таблице 2 есть 1 автомобиль.

JOIN location t2out_loc_to ON t2out.location_to_id = t2out_loc_to.id

и т. Д.

0 голосов
/ 04 марта 2019

попробуйте, как показано ниже, соедините местоположение и автомобиль, используя соответствующий ключ

select t1.other_fields,t2.name as out_name,
     t22.name as in_name,l1.name as location_to_name
    ,l2.name as location_from_name,c.name from 
      table1 t1
      left join table2 t2 on t1.out_path_id=t2.id
      left join table2 t22 on t1.in_path=t22.id
      left join location l1 on t2.location_to_id=l1.id
      left join location l2 on t2.location_from_id=l2.id
      left join car c on t2.car_id=c.id
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...