MySQL 查询不太正确...可能很简单

发布于 2024-11-17 00:48:12 字数 851 浏览 1 评论 0原文

我有一个工资系统表,其中有四个字段和一些示例数据:

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)。那里没有问题。

现在,我试图获取一个查询来查找

  1. 与员工相关的所有条目,然后
  2. 这些子条目中的最大有效日期标记
  3. 与该最大值对应的工资。

我已经做了我的第一个子查询(!),它几乎是正确的,但它有错误。有没有大师可以看看并给我一个正确的方向?

SELECT MAX( effective_date),new_wage FROM (SELECT effective_date,new_wage FROM hr_wages WHERE employee_code='06031-BOB') AS t1

理想情况下,我想要 11022813 被返回。但是,正如前面提到的大师无疑会立即看到的那样,有些事情是错误的。 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

  1. All entries related to an employee, then
  2. The maximum effective_date stamp in those sub-entries
  3. 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 技术交流群。

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

发布评论

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

评论(3

十年九夏 2024-11-24 00:48:12

等等,是什么阻止你做以下事情?

select effective_date, new_wage from hr_wages
where employee_code = '06031-BOB'
order by effective_date desc
limit 1

Hang on, what's stopping you from doing the following?

select effective_date, new_wage from hr_wages
where employee_code = '06031-BOB'
order by effective_date desc
limit 1
梦初启 2024-11-24 00:48:12

仅对于一名员工,您可以将 IN 与子查询

   SELECT new_wage, other data... 
    FROM hr_wages
    WHERE effective_date IN (SELECT max(effective_date) FROM hr_wages WHERE employee_code='06031-BOB')
AND employee_code='06031-BOB' -- Corrected: Credits to Stev

e

一起使用对于所有员工

SELECT new_wage, CO 
FROM hr_wages w LEFT JOIN
(SELECT max(effective_date) effective_date, employee_code 
FROM hr_wages 
GROUP BY employee_code
)d ON w.employee_code = d.employee.code AND w.effective_date = d.effective_date

For only one employee you can use IN with subquery

   SELECT new_wage, other data... 
    FROM hr_wages
    WHERE effective_date IN (SELECT max(effective_date) FROM hr_wages WHERE employee_code='06031-BOB')
AND employee_code='06031-BOB' -- Corrected: Credits to Stev

e

For all employees

SELECT new_wage, CO 
FROM hr_wages w LEFT JOIN
(SELECT max(effective_date) effective_date, employee_code 
FROM hr_wages 
GROUP BY employee_code
)d ON w.employee_code = d.employee.code AND w.effective_date = d.effective_date
献世佛 2024-11-24 00:48:12

@niktrs 有一篇文章提供了正确的答案,但已被删除!

所以,这是我在 @niktrs 帮助下寻找的查询。如果他的帖子回来,我会接受。

SELECT new_wage FROM hr_wages WHERE effective_date IN 
(SELECT effective_date,new_wage FROM hr_wages  WHERE employee_code='06031-BOB') 
AND employee_code='06031-BOB'

我确信这种丑陋是可以美化的。

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.

SELECT new_wage FROM hr_wages WHERE effective_date IN 
(SELECT effective_date,new_wage FROM hr_wages  WHERE employee_code='06031-BOB') 
AND employee_code='06031-BOB'

This ugliness can be beautified, I'm sure.

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