我正在创建一个报告,我需要能够创建一个查询来将下面的信息拉到报表中。从本质上讲,我想列出的是:
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,其中重点列出了有关领域的数据:

发布于 2014-03-07 14:02:11
像这样的怎么样?
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这是查询的长版本。我对此不太确定:左加入crew_position_title c ON c.crew_pos_id = p.personnel_id。我不知道在那里应该使用什么键,也不知道使用命名约定。
查询没有经过测试,但是您可以尝试并修复语法问题(如果有)。
https://stackoverflow.com/questions/22251883
复制相似问题