我是这个领域的新手,我正在制定学费管理系统,按月收取学费,每年等等。
我的MySQL数据库架构如下
academic_classes 表
class_id class_name 1月1日
2 2nd
.....等等
Fee_types 表
fee_type_id fee_name
1 Admission Fee
2 Tuition Fee
3 Sports Fee
class_wise_fee_plan 表格
plan_id class_id fee_id金额 1 1 1 5000
2 1 2 1150
3 1 3 350
费用金额根据班级而定 根据你的建议我添加了一张新表 每年,每月等费用频率
fee_writeoff 表
fee_writeoff_id fee_id个月 1 1 apr
2 2 jan
3 2 feb
依旧......
前端有<12个月的12个复选框,如何根据复选框计算或显示费用值和费用名称。
我想要这种类型的结果
FeeName Apr May Jun ..... Total
入场费5000 0 0 5000
Tution Fee 1100 1100 1100 3300
总计6100 1100 1100 8300如果从前端的复选框中选择月份名称,如何创建mysql存储过程,因为月份名称是逗号saparated如何循环并创建案例
答案 0 :(得分:0)
使用CASE尝试以下查询,它不是您要求的完整解决方案,但这将解决您的一些问题。
SELECT ft.fee_name, (CASE WHEN apr=1 THEN fee_amount ELSE 0 END) AS apr,
(CASE WHEN may=1 THEN fee_amount ELSE 0 END) AS apr,
(CASE WHEN jun=1 THEN fee_amount ELSE 0 END) AS apr,
(CASE WHEN jul=1 THEN fee_amount ELSE 0 END) AS apr,
(CASE WHEN aug=1 THEN fee_amount ELSE 0 END) AS apr,
(CASE WHEN apr=1 THEN fee_amount ELSE 0 END) AS apr,
FROM fee_type ft INNER JOIN fee_plan fp
USING (fee_id)
<强>输出强>
fee_name APR MAY JUNE JULY AUG
Admission Fee 5000 0 0 0 0
Tuition Fee 1150 1150 1150 1150 1150
答案 1 :(得分:0)
首先我要注意,要设计模式,您应该了解关系模型的基础知识。当您将电子表格布局放到关系表中时,您将无法正确使用它。
所以我在关系问题上重新设计了你的模式。它不是唯一可能的架构,但它取决于您的应用程序的其余部分。
模式的
CREATE TABLE `fee` (
`fee_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
PRIMARY KEY (`fee_id`)
) ENGINE = InnoDB;
CREATE TABLE `fee_writeoff` (
`fee_writeoff_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`fee_id` INT UNSIGNED NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`fee_writeoff_id`),
INDEX `fee_id`(`fee_id`),
CONSTRAINT `fee_writeoff_has_fee`
FOREIGN KEY (`fee_id`)
REFERENCES `fee` (`fee_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE TABLE `fee_plan` (
`fee_plan_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`fee_id` INT UNSIGNED NOT NULL,
`amount` DECIMAL(10,0) NOT NULL,
PRIMARY KEY (`fee_plan_id`),
INDEX `fee_id`(`fee_id`),
CONSTRAINT `fee_plan_has_fee`
FOREIGN KEY (`fee_id`)
REFERENCES `fee` (`fee_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE = InnoDB;
数据
INSERT INTO `fee`(`fee_id`, `name`) VALUES
(1, 'Admission Fee'),
(2, 'Tuition Fee');
INSERT INTO `fee_writeoff`(`fee_id`, `date`) VALUES
(1, '2000-04-01'),
(2, '2000-01-01'),
(2, '2000-02-01'),
(2, '2000-03-01'),
(2, '2000-04-01'),
(2, '2000-05-01'),
(2, '2000-06-01'),
(2, '2000-07-01'),
(2, '2000-08-01'),
(2, '2000-09-01'),
(2, '2000-10-01'),
(2, '2000-11-01'),
(2, '2000-12-01');
INSERT INTO `fee_plan`(`fee_id`, `amount`) VALUES
(1, 5000),
(2, 1150);
查询
SELECT
name,
SUM(CASE MONTH(`date`) WHEN 4 THEN amount ELSE 0 END) AS `April`,
SUM(CASE MONTH(`date`) WHEN 5 THEN amount ELSE 0 END) AS `May`,
SUM(CASE MONTH(`date`) WHEN 6 THEN amount ELSE 0 END) AS `June`,
SUM(CASE MONTH(`date`) WHEN 7 THEN amount ELSE 0 END) AS `July`,
SUM(CASE MONTH(`date`) WHEN 8 THEN amount ELSE 0 END) AS `August`,
SUM(CASE MONTH(`date`) WHEN 9 THEN amount ELSE 0 END) AS `September`,
SUM(CASE MONTH(`date`) WHEN 10 THEN amount ELSE 0 END) AS `October`,
SUM(CASE MONTH(`date`) WHEN 11 THEN amount ELSE 0 END) AS `November`,
SUM(CASE MONTH(`date`) WHEN 12 THEN amount ELSE 0 END) AS `December`,
SUM(CASE MONTH(`date`) WHEN 1 THEN amount ELSE 0 END) AS `January`,
SUM(CASE MONTH(`date`) WHEN 2 THEN amount ELSE 0 END) AS `February`,
SUM(CASE MONTH(`date`) WHEN 3 THEN amount ELSE 0 END) AS `March`,
SUM(amount) AS `Total`
FROM fee
JOIN fee_writeoff USING(fee_id)
JOIN fee_plan USING(fee_id)
GROUP BY name WITH ROLLUP