我有三张table
员工:
+----+--------+----------+--------+-----------------+-----+
| id | E_name | super_id | salary | bdate | Dno |
+----+--------+----------+--------+-----------------+-----+
| 1 | John | 3 | 100000 | Fri Jan 01 1960 | 1 |
| 2 | Mary | 3 | 100000 | Tue Dec 01 1964 | 3 |
| 3 | Bob | null | 100000 | Thu Feb 07 1974 | 3 |
| 4 | Tom | 1 | 100000 | Tue Jan 17 1978 | 2 |
| 5 | Bill | null | 100000 | Thu Jan 17 1985 | 1 |
+----+--------+----------+--------+-----------------+-----+
部门:
+-----+--------------+
| Dno | dname |
+-----+--------------+
| 1 | Payroll |
| 2 | Tech support |
| 3 | Research |
+-----+--------------+
部门:
+-----+-------------+
| dno | totalsalary |
+-----+-------------+
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
+-----+-------------+
deptsal表包含按部门划分的工资与dno之和。作为引用部门表的外键。。
我得用mysql写两个程序
我必须编写一个更新部门总工资的过程,该部门的dno在deptsal表中的过程参数中传递
我试过了
DELIMITER $$
CREATE PROCEDURE `updateSalary` (IN dno INT)
BEGIN
UPDATE deptsal
SET totalsalary = (
SELECT SUM(employee.salary)
FROM employee
WHERE employee.dno = @dno
GROUP BY employee.dno
)
where deptsal.dno = dno;
END$$
DELIMITER ;
但这似乎不管用
我还需要编写一个程序来更新整个deptsal表
下面是我的create table查询
CREATE TABLE department (
Dno INT(6) PRIMARY KEY,
dname VARCHAR(25)
);
CREATE TABLE employee (
id INT(6) PRIMARY KEY,
E_name VARCHAR(25),
super_id INT(6),
salary INT(10),
bdate DATE,
Dno INT(6)
);
ALTER TABLE employee
ADD FOREIGN KEY (Dno) REFERENCES department(Dno);
CREATE TABLE deptsal (
dno INT(6) PRIMARY KEY,
totalsalary INT(10)
);
ALTER TABLE deptsal
ADD FOREIGN KEY (Dno) REFERENCES department(Dno);
1条答案
按热度按时间ws51t4hk1#
@dno和dno是两个独立的符号
若要全部更新,可以修改过程,若要在dno=-1时全部更新
或者写第二个程序