gpt4 book ai didi

MySQL 每个派生表都必须有自己的别名

转载 作者:可可西里 更新时间:2023-11-01 06:41:10 25 4
gpt4 key购买 nike

您好,我需要通过连接两个查询来生成一个 sql 查询

第一个查询:获取所有学生数据

SELECT * FROM students where class = 1 and section = 'A'

enter image description here

第二个查询:获取每次出席的次数

SELECT  roll_no,full_name, 

SUM(hasAttended= 'P') AS DaysPresent,

SUM(hasAttended= 'A') AS DaysAbsent,

COUNT(*) AS totalClasses

FROM attendance

GROUP BY roll_no

enter image description here

现在我需要连接两个表并生成一个结果表

我正在尝试以下查询但出现错误:

1248 - Every derived table must have its own alias

我的查询如下:

SELECT * FROM students as st

INNER JOIN

(SELECT att.roll_no,att.full_name,

SUM(att.hasAttended= 'P') AS DaysPresent,

SUM(att.hasAttended= 'A') AS DaysAbsent,

COUNT(*) AS totalClasses

FROM attendance as att

GROUP BY att.roll_no)

ON st.roll_no = att.roll_no

ORDER BY st.roll_no

谁能解决上面的错误

最佳答案

MySQL 要求所有派生表和子查询都有一个别名。您在子查询的右括号末尾缺少别名:

SELECT * 
FROM students as st
INNER JOIN
(
SELECT att.roll_no,att.full_name,
SUM(att.hasAttended= 'P') AS DaysPresent,
SUM(att.hasAttended= 'A') AS DaysAbsent,
COUNT(*) AS totalClasses
FROM attendance as att
GROUP BY att.roll_no
) att ---------------------------< this is missing
ON st.roll_no = att.roll_no
WHERE st.class = 1
ORDER BY st.roll_no

关于MySQL 每个派生表都必须有自己的别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785925/

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