gpt4 book ai didi

sql - 带有子查询的 Oracle sql 查询还是我应该规范化?

转载 作者:行者123 更新时间:2023-12-04 06:49:11 24 4
gpt4 key购买 nike

我有以下查询有效,但我想知道它是否可以更有效。我需要电话簿表 (pb) 中 4 名员工的名字和姓氏,他们的徽章(员工 ID)存储在 Commitment 表中

SELECT Originator_ID, 
(SELECT pb.lname FROM pb WHERE pb.badge = Commitment.Originator_ID) AS Originator_Last_Name,
(SELECT pb.fname FROM pb WHERE pb.badge = Commitment.Originator_ID) AS Originator_First_Name,
Checker_ID,
(SELECT pb.lname FROM pb WHERE pb.badge = Commitment.Checker_ID) AS Checker_Last_Name,
(SELECT pb.fname FROM pb WHERE pb.badge = Commitment.Checker_ID) AS Checker_First_Name,
Reviewer_ID,
(SELECT pb.lname FROM pb WHERE pb.badge = Commitment.Reviewer_ID) AS Reviewer_Last_Name,
(SELECT pb.fname FROM pb WHERE pb.badge = Commitment.Reviewer_ID) AS Reviewer_First_Name,
Approver_ID,
(SELECT pb.lname FROM pb WHERE pb.badge = Commitment.Approver_ID) AS Approver_Last_Name,
(SELECT pb.fname FROM pb WHERE pb.badge = Commitment.Approver_ID) AS Approver_First_Name
FROM Commitment
WHERE Commitment.Approver_ID IN (SELECT pb.badge FROM pb WHERE pb.dept = ?) ORDER BY Commitment_ID

我的查询是否有太多子查询?

或者我应该规范化并将 4 个员工徽章拆分到一个单独的表格中?如果我要规范化,似乎我存储徽章的新表需要某种角色列,然后我是否需要第三个查找表来存储该角色?然后让事情复杂化,我需要使用传入的绑定(bind)变量“dept”通过 Approver_ID 查询 promise 。不知道该走哪条路。
TABLE: commitment_emp
Commitment_ID (PK) (FK) VARCHAR2(10)
badge (PK) VARCHAR2(10)
role (PK) VARCHAR2(20)

最佳答案

采用:

   SELECT c.originator_id, 
orig.lname,
orig.fname,
c.checker_id,
check.lname,
check.fname,
c.reviewer_id,
review.lname,
review.fname,
c.approver_id,
approve.lname,
approve.fname
FROM COMMITMENT c
LEFT JOIN PB orig ON orig.badge = c.originator_id
LEFT JOIN PB check ON check.badge = c.checker_id
LEFT JOIN PB review ON review.badge = c.reviewer_id
JOIN PB approve ON approve.badge = c.approver_id
AND approve.dept ?
ORDER BY c.commitment_id

JOIN 和表别名是你的 friend - 表设计很好。

关于sql - 带有子查询的 Oracle sql 查询还是我应该规范化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3366847/

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