MySQL 查询不太正确...可能很简单
我有一个工资系统表,其中有四个字段和一些示例数据:
pkref employee_id new_wage effective_date
===== =========== ======== ==============
23 06031-BOB 10 080101
37 06031-BOB 15 090501
90 06031-BOB 13 110228
当员工的工资发生变化时,主键引用自动递增,并记录适当的信息(effective_date 是时间戳,yymmdd)。那里没有问题。
现在,我试图获取一个查询来查找
- 与员工相关的所有条目,然后
- 这些子条目中的最大有效日期标记
- 与该最大值对应的工资。
我已经做了我的第一个子查询(!),它几乎是正确的,但它有错误。有没有大师可以看看并给我一个正确的方向?
SELECT MAX( effective_date),new_wage FROM (SELECT effective_date,new_wage FROM hr_wages WHERE employee_code='06031-BOB') AS t1
理想情况下,我想要 110228
和 13
被返回。但是,正如前面提到的大师无疑会立即看到的那样,有些事情是错误的。 new_wage
值并不总是与最大 effective_date
匹配。
所以。增值税要做吗?
I've got a table for a payroll system, with four fields, and some sample data:
pkref employee_id new_wage effective_date
===== =========== ======== ==============
23 06031-BOB 10 080101
37 06031-BOB 15 090501
90 06031-BOB 13 110228
When an employee's wage is changed, a primary key reference auto-increments, and the appropriate information is recorded (effective_date
is a timestamp, yymmdd). No problems there.
Now, I'm trying to get a query to find
- All entries related to an employee, then
- The maximum
effective_date
stamp in those sub-entries - The wage that corresponds to that maximum.
I've made my very first subquery ever (!), got it almost right, but it's buggy. Could some guru have a look and give me a bump in the right direction?
SELECT MAX(effective_date),new_wage FROM (SELECT effective_date,new_wage FROM hr_wages WHERE employee_code='06031-BOB') AS t1
Ideally, I want 110228
and 13
to be returned. But, as the aforementioned guru will no doubt see immediately, something is wrong. The new_wage
value does not always match the max effective_date
.
So. Vat to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
等等,是什么阻止你做以下事情?
Hang on, what's stopping you from doing the following?
仅对于一名员工,您可以将 IN 与子查询
e
一起使用对于所有员工
For only one employee you can use IN with subquery
e
For all employees
@niktrs 有一篇文章提供了正确的答案,但已被删除!
所以,这是我在 @niktrs 帮助下寻找的查询。如果他的帖子回来,我会接受。
我确信这种丑陋是可以美化的。
There was a post from @niktrs that provided the right answer, but it's been deleted!
So, here's the query I was looking for, with @niktrs help. If his post comes back, I'll accept it.
This ugliness can be beautified, I'm sure.