这可能是一个不可能的 MySQL 查询吗?
到目前为止我所拥有的:
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_items
(line_items
表中的外键employee_statements_id
。)。jobs
也有许多line_items
(line_items
表中的外键job_id
)。
(line_items
基本上是 employee_statements
和 jobs
的联接表)
employees_jobs
是多对多的联接表employees
表和jobs
表之间的关系。employee_statements
属于employees
(employee_statements
表中的外键employee_id
)
我想要做什么:
我有一个场景,其中使用行项目创建员工报表。这些行项目引用已完成的作业。每项工作可以分配多名员工。
假设我们有一名 id = 17
的员工。我想要获取尚未与他之前的任何员工报表中的行项目相关联的所有工作。
因此,所有这些都将包含在结果中:
- 员工 17 从事的所有工作均不与行项目关联。
- 员工 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 manyline_items
(foreign keyemployee_statements_id
in theline_items
table.).jobs
have manyline_items
as well (foreign keyjob_id
in theline_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 anemployees
table and thejobs
table.employee_statements
belong toemployees
(foreign keyemployee_id
in theemployee_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:
- All jobs that employee 17 worked on that are not associated with a line item.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说也是漫长的一天,所以我可能会错过一些东西,但是这个怎么样:
A long day for me as well, so I may be missing something, but how about this:
我不太明白你想要什么,但你不应该从这样的开始:
很难说出到底发生了什么,而且这里也度过了漫长的一天,但你现在可能会得到这个:)
看起来就像您需要第二个查询一样,因为这首先只返回与员工 17 相关的工作...
I don't quite get what you want, but shouldn't you start with something like:
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...