MySQL累进税

来源:互联网 发布:张辅 知乎 编辑:程序博客网 时间:2024/06/01 20:38


#用户表
DROP TABLE if EXISTS user1;
CREATE TABLE user1 (
  `id` int(11) NOT NULL auto_increment primary key,
  `user_name` VARCHAR(32) NOT NULL comment '用户名',
  `money` int(11) NOT null comment '工资'
) engine=InnoDB DEFAULT charset=utf8 comment='工资表';


#税率表
DROP TABLE if EXISTS tabRate;
CREATE TABLE tabRate (
  `id` int(11) NOT null auto_increment PRIMARY KEY comment 'id',
  `low` int(11) NOT NULL DEFAULT 0 comment '低',
  `high` int(11) not null DEFAULT 0 comment '高',
  `rate` DECIMAL(3) comment '税率'
) engine=MyISAM DEFAULT charset=utf8 comment='税率表';


#计算累进税
SELECT user_name,sum(curmoney * rate) FROM (
  SELECT user_name,money,low,high,least(money-low,high-low) AS curmoney,rate
    FROM user1 a JOIN tabRate b on a.money > b.low) a
GROUP BY user_name;
原创粉丝点击