Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何根据销售目标确定员工奖金?_Mysql - Fatal编程技术网

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%  |