Mysql 如何使用sql从数据库中的货币中选择零件
请询问我可以使用哪个查询来选择,例如,如果用户的工资为10500,我希望他只能提取10000,而数据库保留剩余余额。我只想让用户在1000的范围内收款。例如,您可以使用mod函数检查面额是否为1000Mysql 如何使用sql从数据库中的货币中选择零件,mysql,Mysql,请询问我可以使用哪个查询来选择,例如,如果用户的工资为10500,我希望他只能提取10000,而数据库保留剩余余额。我只想让用户在1000的范围内收款。例如,您可以使用mod函数检查面额是否为1000 MariaDB [sandbox]> select emp_no,salary, salary % 1000 modsalary from employees; +--------+--------+-----------+ | emp_no | salary | modsalary | +
MariaDB [sandbox]> select emp_no,salary, salary % 1000 modsalary from employees;
+--------+--------+-----------+
| emp_no | salary | modsalary |
+--------+--------+-----------+
| 1 | 20000 | 0 |
| 2 | 39500 | 500 |
| 3 | 50000 | 0 |
| 4 | 19500 | 500 |
| 5 | 10000 | 0 |
| 6 | 19500 | 500 |
| 7 | 40000 | 0 |
| 9 | NULL | NULL |
+--------+--------+-----------+
8 rows in set (0.00 sec)
drop trigger if exists wdl;
delimiter $$
CREATE DEFINER=`root`@`localhost` TRIGGER `wdl` before INSERT ON `employees_wdl` FOR EACH
ROW BEGIN
declare verr int;
declare vsalary int;
set verr = 0;
if not exists(select 1 from employees where emp_no = new.emp_no) then
set verr = 1;
else
select salary into vsalary from employees where emp_no = new.emp_no;
if new.amt > vsalary then set verr = 1; end if;
if new.amt % 1000 <> 0 then set verr = 1 ; end if;
end if;
if verr <> 0 then
signal sqlstate '45000'
set message_text = 'Wdl amt incorrect or no employee exists';
end if;
END $$
delimiter ;
例如,在触发器中捕获错误
MariaDB [sandbox]> select emp_no,salary, salary % 1000 modsalary from employees;
+--------+--------+-----------+
| emp_no | salary | modsalary |
+--------+--------+-----------+
| 1 | 20000 | 0 |
| 2 | 39500 | 500 |
| 3 | 50000 | 0 |
| 4 | 19500 | 500 |
| 5 | 10000 | 0 |
| 6 | 19500 | 500 |
| 7 | 40000 | 0 |
| 9 | NULL | NULL |
+--------+--------+-----------+
8 rows in set (0.00 sec)
drop trigger if exists wdl;
delimiter $$
CREATE DEFINER=`root`@`localhost` TRIGGER `wdl` before INSERT ON `employees_wdl` FOR EACH
ROW BEGIN
declare verr int;
declare vsalary int;
set verr = 0;
if not exists(select 1 from employees where emp_no = new.emp_no) then
set verr = 1;
else
select salary into vsalary from employees where emp_no = new.emp_no;
if new.amt > vsalary then set verr = 1; end if;
if new.amt % 1000 <> 0 then set verr = 1 ; end if;
end if;
if verr <> 0 then
signal sqlstate '45000'
set message_text = 'Wdl amt incorrect or no employee exists';
end if;
END $$
delimiter ;
请告诉我。我根本不明白这一点,我如何在php中应用它请不要,如果这是一个php问题,你应该这样标记。我会在10分钟内删除