我有一个MySQL表,其中包含以下结构和数据:
Increments
id emp_id starting_salary increment_rate increment_frequency
2 340 5000 250 1
3 340 5000 250 4
我需要使用别名a
和b
,它们将根据以下公式保留一些值:
starting_salary + (increment_rate * increment_frequency)
准确地说,我想要a = 5250
(基于a = (5000 + (250 * 1))
)和b = 6000
(基于b = (5000 + (250 * 4))
)
现在我有另一张包含以下数据的表:
PaySlips
id employee_id salary_month arrear
173824 340 '2015-06-01' 2350
我想加入a
和b
,这是我从表Increments
获得的表PaySlips
。我希望以下列方式使用a
和b
:
((a * 8) / 30 + (b * 22) / 30)
我的别名是basic_salary
。因此basic_salary
将保留上述计算中的此值:
basic_salary = ((a * 8) / 30 + (b * 22) / 30)
= ((5250 * 8) / 30 + (6000 *22) / 30)
= (1400 + 4400)
= 5800
我不知道该怎么做。有人可以帮帮我吗?
我得到的只是两个表中的公共列 - emp_id
和employee_id
,我可以使用这些列加入两个表。我无法弄清楚如何存储上述值并在查询中组织计算。
示例查询:
SELECT x.id, x.employee_id,
(*my calculation using a and b from table Increments*) AS basic_salary,
x.salary_month, x.arrear
FROM PaySlips x
JOIN Increments y
ON x.employee_id = y.emp_id
确定a
:
SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS a
FROM Increments
WHERE id = 2
确定b
:
SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS b
FROM Increments
WHERE id = 3
MySQL版本:5.2
答案 0 :(得分:2)
我不清楚所有细节,例如,如果一名员工有三行增量,会发生什么?无论如何,这是一个草图开始:
select employee_id
, ((a * 8) / 30 + (b * 22) / 30) as basic_salary
from (
select x.employee_id
, min(starting_salary + (increment_rate * increment_frequency)) as a
, max(starting_salary + (increment_rate * increment_frequency)) as b
, x.salary_month, x.arrear
from payslips x
join increments y
on x.employee_id = y.emp_id
group by x.employee_id, x.salary_month, x.arrear
) as t
如果id 2和3是标准(我假设它们是示例),您可以使用如下的案例陈述:
select employee_id
, ((a * 8) / 30 + (b * 22) / 30) as basic_salary
from (
select x.employee_id
, max(starting_salary + (increment_rate * case when y.id = 2 then increment_frequency end )) as a
, max(starting_salary + (increment_rate * case when y.id = 3 then increment_frequency end)) as b
, x.salary_month
, x.arrear
from payslips x
join increments y
on x.employee_id = y.emp_id
group by x.employee_id, x.salary_month, x.arrear
) as t;
在这种情况下,使用什么聚合并不重要,它是摆脱包含null的行。
答案 1 :(得分:1)
根据您添加的要求,我认为这样的事情可以解决您的问题:
SELECT PS.id, PS.employee_id, ((A.value * 8) / 30 + (B.value * 22) / 30) AS basic_salary
FROM PaySlips AS PS
JOIN (
SELECT I.emp_id, I.starting_salary + (increment_rate * increment_frequency) AS VALUE
FROM Increments AS I
WHERE I.id = 2
) AS A
ON A.emp_id = PS.employee_id
JOIN (
SELECT I.emp_id, I.starting_salary + (increment_rate * increment_frequency) AS value
FROM Increments AS I
WHERE I.id = 3
) AS B
ON B.emp_id = PS.employee_id
如果版本不适用于您的真实场景,可能需要进行一些更改,但请随时告诉您是否需要修改其他内容。
希望它有所帮助。
答案 2 :(得分:1)
用于确定和设置@a变量:
SET @a := (SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS a
FROM Increments
WHERE id = 2);
用于确定和设置@b变量:
SET @b := (SELECT
(
starting_salary +
(increment_rate * increment_frequency)
) AS b
FROM Increments
WHERE id = 3);
然后您可以在主查询中使用 @a 和 @b ; 你可以通过
进行测试SELECT @a as a;
SELECT @b as b;
答案 3 :(得分:1)
SELECT
x.id,
x.employee_id,
(y.a * 8) / 30 + (y.b * 22) / 30 as basic_salary,
x.salary_month,
x.arrear
FROM PaySlips x
JOIN (
select t1.emp_id, t1.a, t2.b
from (
select
emp_id,
starting_salary + increment_rate * increment_frequency as a
from Increments
where id = 2
) as t1
join (
select
emp_id,
starting_salary + increment_rate * increment_frequency as b
from Increments
where id = 3
) as t2
on t1.emp_id = t2.emp_id
) as y
ON x.employee_id = y.emp_id