MySQL 从查询结果中添加列
MySQL add columns from query results
我想 add/create 基于此特定列结果的 table 列中的三列。这是我在 tblexammarks 中的记录:
SELECT exm.ID,
exm.Admission_No,
exm.Subject AS 'SID',
sb.Name AS 'Subject Name',
exm.Term,
exm.Test,
exm.Marks
FROM tblexammarks exm
INNER JOIN tblsubjects sb ON exm.Subject=sb.ID
ORDER BY exm.ID ASC;
输出:
ID Admission_No SID Subject Name Term Test Marks
1 KBA-2022-003 6 Science Term One Test One 94
2 KBA-2022-003 6 Science Term One Test Two 88
3 KBA-2022-003 6 Science Term One Test Three 78
这是我想要实现的目标:
Admission_No SID Subject Name Term Test One Test Two Test Three Total Marks
KBA-2022-003 6 Science Term One 94 88 78 260
我试过的和输出:
SELECT exm.ID,
exm.Admission_No,
exm.Subject AS 'SID',
sb.Name AS 'Subject Name',
exm.Term,
if(Test='Test One', marks, 0) AS 'Test One',
if(Test='Test Two', marks, 0) AS 'Test Two',
if(Test='Test Three', marks, 0) AS 'Test Three'
FROM tblexammarks exm
INNER JOIN tblsubjects sb ON exm.Subject=sb.ID;
ID Admission_No SID Subject Name Term Test One Test Two Test Three
1 KBA-2022-003 6 Science Term One 94 0 0
2 KBA-2022-003 6 Science Term One 0 88 0
3 KBA-2022-003 6 Science Term One 0 0 78
我试过使用 MySQL IF 函数,但我迷路了。有人可以帮忙吗
您可以尝试使用条件语句 IF
并为三个 Test 字段中的每一个提取单个值,然后使用 MAX
聚合函数删除空值和 SUM
以获得 Marks 字段的总计,然后使用 GROUP BY
子句聚合其余选定字段。
SELECT exm.Admission_No,
exm.Subject AS SID,
sb.Name AS Subject_Name,
exm.Term,
MAX(IF(exm.Test = 'Test One' , 'Test One' , NULL)) AS Test_One,
MAX(IF(exm.Test = 'Test Two' , 'Test Two' , NULL)) AS Test_Two,
MAX(IF(exm.Test = 'Test Three', 'Test Three', NULL)) AS Test_Three,
SUM(exm.Marks) AS Marks
FROM tblexammarks exm
INNER JOIN tblsubjects sb
ON exm.Subject = sb.ID
GROUP BY exm.Admission_No,
exm.Subject AS 'SID',
sb.Name AS 'Subject Name',
exm.Term
ORDER BY exm.ID ASC;
如果您能提供两个表中的示例数据,我可以提供经过全面测试的解决方案。
我想 add/create 基于此特定列结果的 table 列中的三列。这是我在 tblexammarks 中的记录:
SELECT exm.ID,
exm.Admission_No,
exm.Subject AS 'SID',
sb.Name AS 'Subject Name',
exm.Term,
exm.Test,
exm.Marks
FROM tblexammarks exm
INNER JOIN tblsubjects sb ON exm.Subject=sb.ID
ORDER BY exm.ID ASC;
输出:
ID Admission_No SID Subject Name Term Test Marks
1 KBA-2022-003 6 Science Term One Test One 94
2 KBA-2022-003 6 Science Term One Test Two 88
3 KBA-2022-003 6 Science Term One Test Three 78
这是我想要实现的目标:
Admission_No SID Subject Name Term Test One Test Two Test Three Total Marks
KBA-2022-003 6 Science Term One 94 88 78 260
我试过的和输出:
SELECT exm.ID,
exm.Admission_No,
exm.Subject AS 'SID',
sb.Name AS 'Subject Name',
exm.Term,
if(Test='Test One', marks, 0) AS 'Test One',
if(Test='Test Two', marks, 0) AS 'Test Two',
if(Test='Test Three', marks, 0) AS 'Test Three'
FROM tblexammarks exm
INNER JOIN tblsubjects sb ON exm.Subject=sb.ID;
ID Admission_No SID Subject Name Term Test One Test Two Test Three
1 KBA-2022-003 6 Science Term One 94 0 0
2 KBA-2022-003 6 Science Term One 0 88 0
3 KBA-2022-003 6 Science Term One 0 0 78
我试过使用 MySQL IF 函数,但我迷路了。有人可以帮忙吗
您可以尝试使用条件语句 IF
并为三个 Test 字段中的每一个提取单个值,然后使用 MAX
聚合函数删除空值和 SUM
以获得 Marks 字段的总计,然后使用 GROUP BY
子句聚合其余选定字段。
SELECT exm.Admission_No,
exm.Subject AS SID,
sb.Name AS Subject_Name,
exm.Term,
MAX(IF(exm.Test = 'Test One' , 'Test One' , NULL)) AS Test_One,
MAX(IF(exm.Test = 'Test Two' , 'Test Two' , NULL)) AS Test_Two,
MAX(IF(exm.Test = 'Test Three', 'Test Three', NULL)) AS Test_Three,
SUM(exm.Marks) AS Marks
FROM tblexammarks exm
INNER JOIN tblsubjects sb
ON exm.Subject = sb.ID
GROUP BY exm.Admission_No,
exm.Subject AS 'SID',
sb.Name AS 'Subject Name',
exm.Term
ORDER BY exm.ID ASC;
如果您能提供两个表中的示例数据,我可以提供经过全面测试的解决方案。