嗨,我是这个mysql的新手,很难查询pivot类型的数据。
我正在创建学生管理系统。在“费用”模块中,我逐个存储费用条目。为了收取费用,我们在条目中有“level”、“month”和“amount”。所以我需要这个月的报告。详情如下。
费用清单表:
CREATE TABLE fees_inventory ( fees_inventory_id INT(11) NOT NULL AUTO_INCREMENT,
fees_id INT(11) NULL DEFAULT NULL,
student_id INT(11) NOT NULL,
programme_id INT(11) NOT NULL,
fees_level INT(11) NOT NULL,
level_month VARCHAR(10) NOT NULL COLLATE 'utf8mb4_unicode_ci',
fees_amount INT(11) NOT NULL,
fees_type VARCHAR(50) NOT NULL COLLATE 'utf8mb4_unicode_ci',
fees_month VARCHAR(15) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
payable_date DATE NULL DEFAULT NULL,
fees_date DATE NULL DEFAULT NULL,
fees_year YEAR NULL DEFAULT NULL,
PRIMARY KEY (fees_inventory_id),
UNIQUE INDEX Index 5 (student_id, fees_year, fees_month, fees_type, programme_id),
INDEX FK_fees_inventory_programme_master (programme_id),
INDEX FK_fees_inventory_fees_master (fees_id),
CONSTRAINT FK_fees_inventory_fees_master FOREIGN KEY (fees_id) REFERENCES fees_master (fees_id),
CONSTRAINT FK_fees_inventory_programme_master FOREIGN KEY (programme_id) REFERENCES programme_master (programme_id),
CONSTRAINT FK_fees_inventory_student_master FOREIGN KEY (student_id) REFERENCES student_master (student_id) )
COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB AUTO_INCREMENT=350 ;
插入脚本:
INSERT INTO fees_inventory (fees_inventory_id, fees_id, student_id, programme_id, fees_level, level_month, fees_amount, fees_type, fees_month, payable_date, fees_date, fees_year)
VALUES
(30, NULL, 165, 111, 8, '1', 750, 'Term', 'June', '2018-06-01', '2018-06-10', '2018'),
(31, NULL, 165, 111, 8, '2', 750, 'Term', 'July', '2018-07-01', '2018-07-01', '2018'),
(32, NULL, 165, 111, 8, '3', 750, 'Term', 'August', '2018-08-01', '2018-08-05', '2018'), (33, NULL, 150, 111, 4, '3', 750, 'Term', 'June', '2018-06-01', '2018-06-10', '2018'), (34, NULL, 150, 111, 4, '3E', 750, 'Term', 'July', '2018-07-01', '2018-07-01', '2018'), (35, NULL, 150, 111, 5, '1', 750, 'Term', 'August', '2018-08-01', '2018-08-05', '2018'), (36, NULL, 70, 113, 2, '4', 750, 'Term', 'June', '2018-06-01', '2018-06-16', '2018'), (37, NULL, 70, 113, 3, '1', 750, 'Term', 'July', '2018-07-01', '2018-07-18', '2018'), (38, NULL, 173, 111, 2, '1', 750, 'Term', 'June', '2018-06-01', '2018-07-01', '2018'), (39, NULL, 173, 111, 2, '2', 750, 'Term', 'July', '2018-07-01', '2018-07-01', '2018'),
(40, NULL, 198, 111, 7, '1', 750, 'Term', 'June', '2018-06-01', '2018-06-10', '2018'), (41, NULL, 198, 111, 7, '2', 750, 'Term', 'July', '2018-07-01', '2018-07-01', '2018'), (42, NULL, 198, 111, 7, '3', 750, 'Term', 'August', '2018-08-01', '2018-08-05', '2018'), (43, NULL, 67, 131, 1, '1', 850, 'Term', 'June', '2018-06-01', '2018-06-09', '2018'), (44, NULL, 67, 131, 1, '2', 850, 'Term', 'July', '2018-07-01', '2018-07-14', '2018'), (45, NULL, 184, 111, 4, '2', 750, 'Term', 'June', '2018-06-01', '2018-06-10', '2018'), (46, NULL, 184, 111, 4, '3', 750, 'Term', 'July', '2018-07-01', '2018-07-15', '2018'), (47, NULL, 184, 111, 5, '1', 750, 'Term', 'August', '2018-08-01', '2018-08-05', '2018'), (48, NULL, 58, 111, 8, '3E', 750, 'Term', 'June', '2018-06-01', '2018-06-05', '2018'), (49, NULL, 58, 111, 9, '1', 750, 'Term', 'July', '2018-07-01', '2018-07-13', '2018'), (50, NULL, 118, 111, 4, '2', 750, 'Term', 'June', '2018-06-01', '2018-06-11', '2018'), (51, NULL, 118, 111, 4, '3', 750, 'Term', 'July', '2018-07-01', '2018-07-05', '2018'),
(52, NULL, 77, 113, 3, '3', 750, 'Term', 'June', '2018-06-01', '2018-06-09', '2018'), (53, NULL, 77, 113, 3, '4', 750, 'Term', 'July', '2018-07-01', '2018-07-07', '2018'), (54, NULL, 77, 113, 4, '1', 750, 'Term', 'August', '2018-08-01', '2018-08-11', '2018'), (55, NULL, 86, 113, 2, '1', 750, 'Term', 'June', '2018-06-01', '2018-06-09', '2018'), (56, NULL, 86, 113, 2, '2', 750, 'Term', 'July', '2018-07-01', '2018-07-06', '2018'), (57, NULL, 86, 113, 2, '3', 750, 'Term', 'August', '2018-08-01', '2018-08-04', '2018'), (58, NULL, 102, 111, 1, '1', 850, 'Term', 'July', '2018-07-01', '2018-07-07', '2018'), (59, NULL, 102, 111, 1, '2', 850, 'Term', 'August', '2018-08-01', '2018-08-09', '2018'), (60, NULL, 146, 111, 3, '1', 750, 'Term', 'June', '2018-06-01', '2018-07-07', '2018'), (61, NULL, 185, 111, 4, '2', 750, 'Term', 'June', '2018-06-01', '2018-06-07', '2018'), (62, NULL, 185, 111, 4, '3', 750, 'Term', 'July', '2018-07-01', '2018-07-05', '2018'), (63, NULL, 185, 111, 5, '1', 750, 'Term', 'August', '2018-08-01', '2018-08-05', '2018'),
(64, NULL, 174, 111, 2, '1', 750, 'Term', 'June', '2018-06-01', '2018-07-01', '2018'), (65, NULL, 174, 111, 2, '2', 750, 'Term', 'July', '2018-07-01', '2018-07-01', '2018'), (66, NULL, 175, 111, 2, '1', 750, 'Term', 'June', '2018-06-01', '2018-07-01', '2018'), (243, NULL, 191, 111, 4, '3E', 750, 'Term', 'August', '2018-08-01', '2018-08-17', '2018');
为获取数据而创建的查询:
SELECT sm.student_id, sm.student_name, spm.`status`,
CONCAT(
CAST(SUM(if(fi.payable_date = '2018-6-01', fi.fees_amount, null)) as CHAR(50)),'_',
CAST(fi.fees_level as CHAR(50)),'_',CAST(fi.level_month as CHAR(50))) as `June`,
CONCAT(
CAST(SUM(if(fi.payable_date = '2018-7-01', fi.fees_amount, null)) as CHAR(50)),'_',
CAST(fi.fees_level as CHAR(50)),'_',CAST(fi.level_month as CHAR(50))) as `July`,
CONCAT(CAST(SUM(if(fi.payable_date = '2018-8-01', fi.fees_amount, null)) as CHAR(50)),'_',
CAST(fi.fees_level as CHAR(50)),'_',
CAST(fi.level_month as CHAR(50))) as `August`
from student_master sm
inner join student_programme_mapping spm
on spm.student_id=sm.student_id
and spm.programme_id in (111,113,131)
left join fees_inventory fi
on fi.student_id=sm.student_id
and fi.programme_id=spm.programme_id
and fi.fees_type='Term'
group by sm.student_id
注意:这里是 fi.fees_level
以及 fi.level_month
在所有列中重复。请帮助我得到每个费用条目的级别和月份。
暂无答案!
目前还没有任何答案,快来回答吧!