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

发布于 2024-09-12 05:32:33 字数 1307 浏览 2 评论 0原文

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

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 个员工徽章分解到一个单独的表中?如果我要标准化,似乎我存储徽章的新表需要某种角色列,然后我是否需要该角色的第三个查找表?然后,让事情变得复杂的是,我需要使用传入的绑定变量“dept”按 Approver_ID 查询承诺。不确定该走哪条路。

TABLE: commitment_emp
Commitment_ID (PK) (FK) VARCHAR2(10)
badge (PK) VARCHAR2(10)
role (PK) VARCHAR2(20)

I have the follwoing query which works but I'm wondering if it could be more efficient. I need the first and last name of the 4 employees from the phonebook table (pb) who's badges (the employees ID) are stored in the Commitment table

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  

Does my query have too many subqueries?

Or should I normalized and break out the 4 employee badges into a separate table? If I were to normalize, it seems my new table to store the badges would need some sort of role column and then would I need a third lookup table for the role?? And then to complicate things, I need to query for Commitments by Approver_ID using the passed in bound variable 'dept'. Not sure which way to go.

TABLE: commitment_emp
Commitment_ID (PK) (FK) VARCHAR2(10)
badge (PK) VARCHAR2(10)
role (PK) VARCHAR2(20)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分開簡單 2024-09-19 05:32:33

使用:

   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 和表别名是你的朋友 - 表设计很好。

Use:

   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

JOINs and table aliases are your friends - table design is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文