我希望在MySQL中编写一些代码,将代码表的第一列转换为行,并从同一个表的第二列获取数据,以计算另一个表中列的百分比结果。
例如:
payroll_structure:
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| percentage | int(3) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
这是我希望获取的第一个表,并希望将payroll.name
列中的所有数据转换为行,以形成数据表结果的标头。
我还希望获取百分比列中的所有数据,然后使用它们来计算另一个表列的百分比结果。
工资:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| amount | double(19,2) | YES | | NULL | |
| staff_id | int(11) | NO | MUL | NULL | |
+----------+--------------+------+-----+---------+----------------+
如果我运行此代码
SELECT staff.id
, staff.fullname AS StaffName
, (SELECT payroll_structure.percentage
FROM payroll_structure
WHERE payroll_structure.name = 'Basic') / 100 * salaries.amount AS Basic
, (SELECT payroll_structure.percentage
FROM payroll_structure
WHERE payroll_structure.name = 'Housing') / 100 * salaries.amount AS Housing
FROM staff
, salaries
WHERE staff.id = salaries.staff_id
ORDER
BY salaries.staff_id ASC
LIMIT 5;
我得到了这个结果:
+----+-------------------------+-------------+-------------+
| id | StaffName | Basic | Housing |
+----+-------------------------+-------------+-------------+
| 1 | Staff 1 | 120000.0000 | 100000.0000 |
| 2 | Staff 2 | 105000.0000 | 87500.0000 |
| 3 | Staff 3 | 90000.0000 | 75000.0000 |
| 4 | Staff 4 | 90000.0000 | 75000.0000 |
| 5 | Staff 5 | 75000.0000 | 62500.0000 |
+----+-------------------------+-------------+-------------+
看着我的代码:
(SELECT payroll_structure.percentage FROM payroll_structure WHERE payroll_structure.name = 'Basic') / 100 * salaries.amount AS Basic,
(SELECT payroll_structure.percentage FROM payroll_structure WHERE payroll_structure.name = 'Housing') / 100 * salaries.amount AS Housing
无论如何,可以编写代码来生成带有WHERE子句的payroll.name
列中的值,这样每当在payroll_structure.name
列中插入新记录并且payroll_structure.percentage
它自动反映时结果是。
例如,我们假设传输已添加到payroll_structure
和payroll_structure.name = 'Transport' and
payroll_structure.percentage ='20'`除以代码中所写的100
SELECT staff.id
, staff.fullname AS StaffName
, (SELECT payroll_structure.percentage
FROM payroll_structure
WHERE payroll_structure.name = 'Basic') / 100 * salaries.amount AS Basic
, (SELECT payroll_structure.percentage
FROM payroll_structure
WHERE payroll_structure.name = 'Housing') / 100 * salaries.amount AS Housing
, (SELECT payroll_structure.percentage
FROM payroll_structure
WHERE payroll_structure.name = 'Transport') / 100 * salaries.amount AS Transport
FROM staff
, salaries
WHERE staff.id = salaries.staff_id
ORDER
BY salaries.staff_id ASC
LIMIT 5;`
+----+-------------------------+-------------+-------------+-------------+
| id | StaffName | Basic | Housing | Transport |
+----+-------------------------+-------------+--------------+-------------+
| 1 | Staff 1 | 120000.0000 | 100000.0000 | 80000.0000 |
| 2 | Staff 2 | 105000.0000 | 87500.0000 | 71500.0000 |
| 3 | Staff 3 | 90000.0000 | 75000.0000 | 60000.0000 |
| 4 | Staff 4 | 90000.0000 | 75000.0000 | 60000.0000 |
| 5 | Staff 5 | 75000.0000 | 62500.0000 | 1500.0000 |
+----+-------------------------+-------------+--------------+-------------+
有没有办法编写代码而不是为payroll_structure.name
和payroll_structure.percentage