gpt4 book ai didi

mysql - Left Outer Join (MySQL) 问题,这个查询有什么问题?

转载 作者:行者123 更新时间:2023-12-01 00:39:19 25 4
gpt4 key购买 nike

我在 mySQL 中有五个表,我正在研究学生费用模块,但我在查询时遇到了一些问题,所以我无法获得适当的结果,所以请帮助我,如果你能给我一些反馈,我将不胜感激在此查询上。

1.a )class_details 表创建

CREATE TABLE `class_details` 
(`class_id_pk` int(11) NOT NULL AUTO_INCREMENT
,`class_name`varchar(200) NOT NULL
,`session` varchar(50) DEFAULT NULL
,`class_status` varchar(50) DEFAULT NULL
,PRIMARY KEY (`class_id_pk`)
,UNIQUE KEY `UNIQUE` (`class_name`,`session`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

1.b) class_details 插入

insert  into `class_details`
(`class_id_pk`,`class_name`,`session`,`class_status`)
VALUES
(1,'1st','2016-2017',NULL)
,(2,'2nd','2016-2017',NULL)
,(3,'3rd','2016-2017',NULL);

2.a) feedetails表创建

CREATE TABLE `feedetails` 
(`section_id_fk` int(50) NOT NULL
,`fees` varchar(30) DEFAULT NULL
,PRIMARY KEY (`section_id_fk`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2.b) feedetails 插入

insert  into `feedetails`(`section_id_fk`,`fees`) 
values
(1,'1000')
,(2,'2000')
,(3,'3000')
,(4,'4000')
,(5,'5000')
,(6,'6000');

3.a) section_details 创建

CREATE TABLE `section_details` 
(`section_id_pk` int(11) NOT NULL AUTO_INCREMENT
,`class_id_fk` int(11) NOT NULL
,`section_name` varchar(50) NOT NULL
,`section_status` varchar(50) DEFAULT NULL
,PRIMARY KEY (`section_id_pk`,`class_id_fk`,`section_name`)
,UNIQUE KEY `UNIQUE` (`class_id_fk`,`section_name`)
,CONSTRAINT `FK_section_details` FOREIGN KEY (`class_id_fk`) REFERENCES `class_details` (`class_id_pk`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

3.b) section_details 插入

insert  into `section_details` (`section_id_pk`,`class_id_fk`,`section_name`,`section_status`) 
values
(1,1,'A',NULL)
,(2,2,'A',NULL)
,(3,3,'A',NULL);

4.a) student_fee

CREATE TABLE `student_fee` 
( `sr_no` int(200) NOT NULL AUTO_INCREMENT
,`scholar_no`int(50) NOT NULL
,`paid_amount` int(200) DEFAULT NULL
,`due_amount` int(200) DEFAULT NULL
,`fee_date` date DEFAULT NULL
,`section_id_fk` int(50) DEFAULT NULL
,PRIMARY KEY (`sr_no`)
,KEY `FK_student_fee`(`section_id_fk`)
,CONSTRAINT `FK_student_fee` FOREIGN KEY (`section_id_fk`) REFERENCES `section_details` (`section_id_pk`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

4.b) student_fee 插入

insert  into student_fee` (`sr_no`,`scholar_no`,`paid_amount`,`due_amount`,`fee_date`,`section_id_fk`) 
values
(3,5,800,200,'2016-06-16',1)
,(4,29,1000,0,'2016-06-16',1)
,(5,5,200,0,'2016-06-16',1);

5.a) student_details 创建

CREATE TABLE `student_details` 
(`scholar_no` int(30) NOT NULL
,`fname` varchar(30) DEFAULT NULL
,`lname` varchar(30) DEFAULT NULL
,`stu_class` varchar(30) DEFAULT NULL
,`rte` varchar(30) DEFAULT NULL
,`active` varbinary(10) DEFAULT NULL
,PRIMARY KEY (`scholar_no`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;

5.b) student_details 插入

insert  into `student_details` (`scholar_no`,`fname`,`lname`,`stu_class`,`rte`,`active`) 
values
(5,'KP','PK','1','N','y')
,(29,'Abc','Xyz','1','N','y');

上面我已经提到了用于此查询的所有表详细信息。第一次在 student_details 表 stu_class=1 中,费用为 1000。当我在 student_fee 中插入一些值并使用我的查询时,结果是正确的,但是当我在 student_fee 中插入一些金额给另一个学生时,查询添加了支付金额对于不正确的同一个学生,我想显示那些实际插入的学生的插入费用..

SELECT 
student_details.scholar_no
,student_details.fname
,student_details.lname
,student_details.stu_class
,feedetails.fees
,class_name
,section_name
,IF(sssf.paid_amount IS NULL,0,sssf.paid_amount) AS paid_amount
FROM
(student_details
LEFT OUTER JOIN feedetails
ON student_details.stu_class = feedetails.section_id_fk
)
LEFT OUTER JOIN
(SELECT
scholar
, SUM(pa) AS paid_amount
, SUM(pva) AS prev_paid_amount
,SUM(da) AS due_amount
, SUM(dva) AS prev_due_amount
,section_id_fk
,fee_date
,stu_class
FROM
(SELECT
scholar
,CASE WHEN section_id_fk = stu_class THEN paid_amount ELSE 0 END AS pa
,CASE WHEN section_id_fk != stu_class THEN paid_amount ELSE 0 END AS pva
,CASE WHEN section_id_fk = stu_class THEN due_amount ELSE 0 END AS da
,CASE WHEN section_id_fk != stu_class THEN due_amount ELSE 0 END AS dva
,due_amount
,paid_amount
,section_id_fk
,fee_date
,stu_class
FROM
(SELECT
scholar
,due_amount
,SUM(paid_amount) AS paid_amount
,section_id_fk
,fee_date
FROM
(SELECT
student_fee.due_amount AS due_amount
,student_fee.paid_amount AS paid_amount
,student_fee.scholar_no AS scholar
,section_id_fk
,fee_date
FROM student_fee
ORDER BY STR_TO_DATE(fee_date,'%Y-%m-%d')DESC
) AS kkk
GROUP BY kkk.scholar,section_id_fk ORDER BY scholar
) AS k
LEFT OUTER JOIN student_details sd
ON k.scholar = sd.scholar_no
) AS lk
) AS sssf
ON student_details.scholar_no = sssf.scholar
LEFT OUTER JOIN
(SELECT *
FROM section_details AS sd
LEFT OUTER JOIN class_details cd
ON sd.class_id_fk = cd.class_id_pk
) AS sc
ON student_details.stu_class = sc.section_id_pk
WHERE student_details.active = 'y' AND rte = 'N'

最佳答案

复杂的查询。不幸的是有缺陷。

首先,您可以从子查询中删除所有 ORDER BY 子句。子查询只是返回无序集,因此无论它们是否包含 ORDER BY 子句都没有影响(除了可能给 DBMS 带来不必要的工作)。

最里面的子查询 (kkk) 没有 WHERE 子句,也没有 GROUP BY 子句,所以你也可以直接从 student_fee 中选择.

下一个子查询 (kk) 按 scholarsection_id_fk 分组,但是你选择了 due_amountfee_date 而没有任何聚合。这为您提供了任意选择的值。不应该是 sum(due_amount)max(due_amount) 之类的吗?

然后在 sssf 子查询中没有 GROUP BY 子句。这只为您提供了一个结果行。但是您选择了未聚合的 scholarsection_id_fkfee_datestu_class,因此您再次获得任意选择的值,例如诸学者之一。

检查所有聚合。这可能有助于设置 ONLY_FULL_GROUP_BY 模式,以避免错误。

关于mysql - Left Outer Join (MySQL) 问题,这个查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com