这可能是一个不可能的 MySQL 查询吗?

发布于 2024-10-03 15:00:28 字数 1875 浏览 2 评论 0原文

到目前为止我所拥有的:

   SELECT jobs.id
     FROM jobs
LEFT JOIN employees_jobs
       ON employees_jobs.job_id = jobs.id
LEFT JOIN line_items
       ON line_items.job_id = employees_jobs.job_id
LEFT JOIN employee_statements
       ON line_items.employee_statement_id = employee_statements.id
    WHERE (
              employee_statements.employee_id != 17 OR
              employee_statements.employee_id IS NULL
          )
      AND employees_jobs.employee_id = 17
 GROUP BY jobs.id
 ORDER BY jobs.delivery_date ASC
  • employee_statements有许多line_itemsline_items表中的外键employee_statements_id。)。
  • jobs 也有许多 line_itemsline_items 表中的外键 job_id)。

line_items 基本上是 employee_statementsjobs 的联接表)

  • employees_jobs 是多对多的联接表employees 表和 jobs 表之间的关系。
  • employee_statements 属于 employeesemployee_statements 表中的外键 employee_id

我想要做什么:

我有一个场景,其中使用行项目创建员工报表。这些行项目引用已完成的作业。每项工作可以分配多名员工。

假设我们有一名 id = 17 的员工。我想要获取尚未与他之前的任何员工报表中的行项目相关联的所有工作。

因此,所有这些都将包含在结果中:

  1. 员工 17 从事的所有工作均不与行项目关联。
  2. 员工 17 从事的所有工作均与其他员工报表上的行项目相关联。

因此,如果员工 17 和员工 14 都从事同一工作,并且员工 14 的报表之一已经包含该工作的行项目(但员工 17 的报表没有该行项目),则该工作应该显示在我的查询结果中。

让我困惑的地方是:

上面的查询返回员工 17 从事的所有工作,无论它们是否已分配给任何人的行项目。

我明白为什么。

在条件逻辑中,我检查employee_statements.employee_id IS NULL。这将返回不在任何报表上的作业。

我还检查 employee_statements.employee_id != 17。这适用于其他员工报表上的工作。

因此,实际上,我在结果中获得了他从事的所有工作,无论他是否已分配给自己一项工作(因为如果另一名员工已分配该工作,则第二个条件会抓住它)。我不知道如何表达才能返回我想要的工作。 (这是漫长的一天。;))

What I have so far:

   SELECT jobs.id
     FROM jobs
LEFT JOIN employees_jobs
       ON employees_jobs.job_id = jobs.id
LEFT JOIN line_items
       ON line_items.job_id = employees_jobs.job_id
LEFT JOIN employee_statements
       ON line_items.employee_statement_id = employee_statements.id
    WHERE (
              employee_statements.employee_id != 17 OR
              employee_statements.employee_id IS NULL
          )
      AND employees_jobs.employee_id = 17
 GROUP BY jobs.id
 ORDER BY jobs.delivery_date ASC
  • employee_statements have many line_items (foreign key employee_statements_id in the line_items table.).
  • jobs have many line_items as well (foreign key job_id in the line_items table).

(line_items is basically a join table for employee_statements and jobs)

  • employees_jobs is a join table for a many to many relationship between an employees table and the jobs table.
  • employee_statements belong to employees (foreign key employee_id in the employee_statements table)

What I'm trying to do:

I have a scenario where employee statements are created with line items. These line items reference jobs that were completed. Multiple employees can be assigned to each job.

Say we have an employee with id = 17. I want to get all jobs that have not been associated with a line item on any of his previous employee statements yet.

So, all of these would be included in the results:

  1. All jobs that employee 17 worked on that are not associated with a line item.
  2. All jobs that employee 17 worked on that have been associated with a line item on a different employee's statement.

So if employee 17 and employee 14 both worked on the same job, and one of employee 14's statements already has a line item for that job (but employee 17's statements do not have that line item), the job should show up in my query result.

Where I'm stumped:

The above query returns all jobs that employee 17 worked on, regardless of whether they have been assigned to anyone's line Items.

I understand why.

In the conditional logic, I check if the employee_statements.employee_id IS NULL. This returns jobs that are not on any statements.

I also check for employee_statements.employee_id != 17. This is for jobs that are on other employee's statements.

So, in effect I'm getting all jobs that he worked on in the results, regardless of whether he has had one assigned to himself (because if another employee has it assigned, the second condition grabs it). I can't figure out how to word this to return just the jobs I want. (it's been a long day. ;))

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

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

发布评论

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

评论(2

┈┾☆殇 2024-10-10 15:00:28

对我来说也是漫长的一天,所以我可能会错过一些东西,但是这个怎么样:

SELECT jobs.id
     FROM jobs
INNER JOIN employees_jobs
       ON employees_jobs.job_id = jobs.id
WHERE NOT EXISTS (SELECT 'X' FROM line_items
                   INNER JOIN employee_statements
                     ON line_items.employee_statement_id = employee_statements.id
                     AND employee_statements.employee_id = 17
                   WHERE line_items.job_id = employees_jobs.job_id)
AND employee_jobs.employee_id = 17
GROUP BY jobs.id
ORDER BY jobs.delivery_date ASC

A long day for me as well, so I may be missing something, but how about this:

SELECT jobs.id
     FROM jobs
INNER JOIN employees_jobs
       ON employees_jobs.job_id = jobs.id
WHERE NOT EXISTS (SELECT 'X' FROM line_items
                   INNER JOIN employee_statements
                     ON line_items.employee_statement_id = employee_statements.id
                     AND employee_statements.employee_id = 17
                   WHERE line_items.job_id = employees_jobs.job_id)
AND employee_jobs.employee_id = 17
GROUP BY jobs.id
ORDER BY jobs.delivery_date ASC
肩上的翅膀 2024-10-10 15:00:28

我不太明白你想要什么,但你不应该从这样的开始:

(employee_statements.employee_id = 17 AND
/* SOME QUERY HERE TO DETERMINE IF NOT ASSOCIATED */)
OR
(employee_statements.employee_id = 17 AND
/* SOME QUERY HERE TO DETERMINE IF ASSOCIATED WITH OTHER EMPLOYEE STATEMENT */ )

很难说出到底发生了什么,而且这里也度过了漫长的一天,但你现在可能会得到这个:)

看起来就像您需要第二个查询一样,因为这首先只返回与员工 17 相关的工作...

I don't quite get what you want, but shouldn't you start with something like:

(employee_statements.employee_id = 17 AND
/* SOME QUERY HERE TO DETERMINE IF NOT ASSOCIATED */)
OR
(employee_statements.employee_id = 17 AND
/* SOME QUERY HERE TO DETERMINE IF ASSOCIATED WITH OTHER EMPLOYEE STATEMENT */ )

It's hard to tell what exactly is going on, and it's been a long day here too, but you could probably get this now :)

It looks like you would need a second query as this is only returning jobs associated with employee 17 in the first place...

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