首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建包含所有这些信息的MySQL查询?

如何创建包含所有这些信息的MySQL查询?
EN

Stack Overflow用户
提问于 2014-03-07 13:49:00
回答 1查看 101关注 0票数 0

我正在创建一个报告,我需要能够创建一个查询来将下面的信息拉到报表中。从本质上讲,我想列出的是:

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,其中重点列出了有关领域的数据:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-07 14:02:11

像这样的怎么样?

代码语言:javascript
复制
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。我不知道在那里应该使用什么键,也不知道使用命名约定。

查询没有经过测试,但是您可以尝试并修复语法问题(如果有)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22251883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档