gpt4 book ai didi

php - 如何创建 MySQL 查询以包含所有这些信息?

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:51 26 4
gpt4 key购买 nike

我正在创建报告,我需要能够创建查询以将以下信息提取到报告中。基本上这是我想要列出的内容:

   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 personal.currently_serving_ship_id IS NOT NULL

我将如何着手创建查询来为我提供所需的数据?我尝试过使用 JOIN,但我对它们没有太多经验。

下面是我需要数据的表的 ERD,突出显示了感兴趣的字段:

database_erd

最佳答案

怎么样?

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。我不知道应该在那里使用什么 key ,无法弄清楚使用命名约定。

查询未经过测试,但您可以试一试并修复语法问题(如果有的话)。

关于php - 如何创建 MySQL 查询以包含所有这些信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22251883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com