我正在创建一个报告,我需要能够创建一个查询以将下面的信息提取到报告中。基本上这就是我想要列出的内容:
ship.name personnel.first_name personnel.last_name crew_position_title.title personnel_next_of_kin.next_of_kin_relation personnel_next_of_kin.next_of_kin_first_name personnel_next_of_kin.next_of_kin_last_name personnel_next_of_kin.next_of_kin_telephone personnel_next_of_kin.next_of_kin_alt_telephone personnel_next_of_kin.other_kin_relation personnel_next_of_kin.other_kin_first_name personnel_next_of_kin.other_kind_last_name personnel_next_of_kin.other_kin_telephone personnel_next_of_kin.other_kin_alt_telephone
人员表上唯一的条件是WHERE personnel.currently_serving_ship_id IS NOT NULL
。
我如何创建查询以向我提供所需的数据?我尝试过使用JOIN,但我对它们没有多少经验。
下面是表格的ERD,我需要突出显示感兴趣的字段的数据:
答案 0 :(得分:3)
怎么样?
SELECT s.name, p.first_name, p.last_name, c.title, pnk.next_of_kin_relation,
pnk.next_of_kin_first_name, pnk.next_of_kin_last_name, pnk.next_of_kin_telephone,
pnk.next_of_kin_alt_telephone, pnk.other_kin_relation, pnk.other_kin_first_name,
pnk.other_kin_last_name, pnk.other_kin_telephone, pnk.other_kin_alt_telephone
FROM personnel p
LEFT JOIN personnel_next_of_kin pnk ON pnk.personnel_id = p.personnel_id
LEFT JOIN ship s ON s.ship_id = p.currently_serving_ship_id
LEFT JOIN personnel_key_info pk ON pk.personnel_id = p.personnel_id
LEFT JOIN crew_position_title c ON c.crew_pos_id = p.personnel_id
WHERE p.currently_serving_ship_id IS NOT NULL
这是查询的长版本。我对此不太确定:LEFT JOIN crew_position_title c ON c.crew_pos_id = p.personnel_id。我不知道应该在那里使用什么密钥,无法弄清楚使用命名约定。
查询未经过测试,但如果有任何问题,您可以试一试并解决语法问题。