Mysql 如何根据销售目标确定员工奖金?
我需要找到那些达到销售目标的员工的身份证,并根据他们的销售目标确定他们每个人应该获得多少奖金Mysql 如何根据销售目标确定员工奖金?,mysql,Mysql,我需要找到那些达到销售目标的员工的身份证,并根据他们的销售目标确定他们每个人应该获得多少奖金 create table ta( empid int(10), sales int(10) ); empid - sales 101 - 9898 201 - 988 301 - 87988 401 - 88 501 - 202088 create table tb( target int(10), bonus varchar(100)); Target -
create table ta(
empid int(10),
sales int(10)
);
empid - sales
101 - 9898
201 - 988
301 - 87988
401 - 88
501 - 202088
create table tb(
target int(10),
bonus varchar(100));
Target - Bonus
1000 - 25%
5000 - 50%
10000 - 70%
50000 - 85%
100000 - 100%
=================================
我试过了
select ta.*
from ta
right outer join tb on ta.sales=tb.target
where sales <= bonus;
选择ta*
来自助教
右外部连接ta上的tb.sales=tb.target
在销售中,您可以使用分组方式使用以下解决方案:
SELECT ta.empid, ta.sales, MAX(tb.target) AS target, CONCAT(MAX(REPLACE(tb.bonus, '%', '') + 0), '%') AS bonus
FROM ta LEFT JOIN tb ON tb.target <= ta.sales
GROUP BY ta.empid, ta.sales
ORDER BY ta.empid
首先获取员工已实现的所有目标,然后获取最高目标:
select t.empid, t.target, tb.bonus from (
select empid, max(target) target
from (
select *
from ta inner join tb
on tb.target <= ta.sales
) g
group by empid
) t inner join tb on tb.target = t.target
两个表中相同的[primary->foreign]列在哪里进行联接?给定的查询有什么问题?“我需要为那些达到销售目标的员工查找员工ID,并根据他们的销售目标确定每个人应该获得多少奖金。”我的意思是,预期的结果是什么?您应该创建一个预期的输出结果集。
SELECT ta.empid, ta.sales, MAX(tb.target) AS target, CONCAT(MAX(REPLACE(tb.bonus, '%', '') + 0), '%') AS bonus
FROM ta LEFT JOIN tb ON tb.target <= ta.sales
GROUP BY ta.empid, ta.sales
HAVING MAX(tb.target) IS NOT NULL -- or just using INNER JOIN
ORDER BY ta.empid
select t.empid, t.target, tb.bonus from (
select empid, max(target) target
from (
select *
from ta inner join tb
on tb.target <= ta.sales
) g
group by empid
) t inner join tb on tb.target = t.target
| empid | target | bonus |
| ----- | ------ | ----- |
| 101 | 5000 | 50% |
| 301 | 50000 | 85% |
| 501 | 100000 | 100% |