所以我有一个包含三个表的数据库,并且对SQL语法的了解可以忽略不计

表格1;

  person_id;
  person_name;


表2;

  thing_id;
  thing_name;


表3;

  action_id;
  action name;
  thing_id; (referencing thing_id in the table 2)
  person1_id(referencing person_id in the Table 1);
  person2_id(referencing person_id in the Table 1);


基本上,我需要组合显示第一人称,第二人称,动作名和事物名。我可以为此采用某种正确的加入方式吗?

最佳答案

使用LEFT JOIN

select
    action_id,
    action name,
    a1.thing_name,
    p1.person_name as person_name1,
    p2.person_name as person_name2
from table3
left join table1 p1 on p1.person_id = table3.person1_id
left join table1 p2 on p2.person_id = table3.person2_id
left join table2 a1 on a1.thing_id = table3.thing_id

10-08 06:49