MySQL 查询失败
这个 MySQL 查询失败了,我只是不明白为什么。感谢您的帮助,如果您需要更多信息,请告诉我。
新查询和新错误
select *
from
(select
classId as `Class ID`, mjla_db.StudentRecordTable2.studentId as `Student ID`, quizId as `Quiz ID`, quizGrade as `Quiz Grade`, mjla_db.StudentTable2.lastName, mjla_db.StudentTable2.firstName
from mjla_db.StudentRecordTable2, mjla_db.StudentTable2
where (mjla_db.StudentRecordTable2.studentId=mjla_db.StudentTable2.studentId))
as A
where classId
in (select mjla_db.ClassTable2.classId
from mjla_db.ClassTable2
where mjla_db.ClassTable2.teacherId='T1');
ERROR 1054 (42S22): Unknown column 'classId' in 'IN/ALL/ANY subquery'
select *
from
(select
classId as 'Class ID',
studentId as 'Student ID',
quizId as 'Quiz ID',
quizGrade as 'Quiz Grade',
mjla_db.StudentTable2.lastName,
mjla_db.StudentTable2.firstName
from
mjla_db.StudentRecordTable2,
mjla_db.StudentTable2
where
(mjla_db.StudentRecordTable2.studentId = mjla_db.StudentTable2.studentId)
) as A
where
A.classId in
(select
mjla_db.ClassTable2.classId
from
mjla_db.ClassTable2
where
mjla_db.ClassTable2.teacherId='T1'
);
describe StudentRecordTable2;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| classId | varchar(20) | NO | MUL | NULL | |
| studentId | varchar(20) | NO | MUL | NULL | |
| quizGrade | tinyint(4) | YES | | NULL | |
| quizId | int(11) | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
describe ClassTable2;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| classId | varchar(20) | NO | PRI | NULL | |
| className | varchar(10) | NO | | NULL | |
| classSection | varchar(5) | NO | | NULL | |
| classSemester | varchar(2) | NO | | NULL | |
| classYear | varchar(4) | NO | | NULL | |
| teacherId | varchar(20) | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
describe StudentTable2;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| studentId | varchar(20) | NO | PRI | NULL | |
| lastName | varchar(45) | NO | | NULL | |
| firstName | varchar(45) | NO | | NULL | |
| studentPassword | varchar(32) | NO | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
This MySQL query is failing and i just can not figure out why. Any help is appreciated and if you need more information, please let me know.
NEW QUERY and NEW ERROR
select *
from
(select
classId as `Class ID`, mjla_db.StudentRecordTable2.studentId as `Student ID`, quizId as `Quiz ID`, quizGrade as `Quiz Grade`, mjla_db.StudentTable2.lastName, mjla_db.StudentTable2.firstName
from mjla_db.StudentRecordTable2, mjla_db.StudentTable2
where (mjla_db.StudentRecordTable2.studentId=mjla_db.StudentTable2.studentId))
as A
where classId
in (select mjla_db.ClassTable2.classId
from mjla_db.ClassTable2
where mjla_db.ClassTable2.teacherId='T1');
ERROR 1054 (42S22): Unknown column 'classId' in 'IN/ALL/ANY subquery'
select *
from
(select
classId as 'Class ID',
studentId as 'Student ID',
quizId as 'Quiz ID',
quizGrade as 'Quiz Grade',
mjla_db.StudentTable2.lastName,
mjla_db.StudentTable2.firstName
from
mjla_db.StudentRecordTable2,
mjla_db.StudentTable2
where
(mjla_db.StudentRecordTable2.studentId = mjla_db.StudentTable2.studentId)
) as A
where
A.classId in
(select
mjla_db.ClassTable2.classId
from
mjla_db.ClassTable2
where
mjla_db.ClassTable2.teacherId='T1'
);
describe StudentRecordTable2;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| classId | varchar(20) | NO | MUL | NULL | |
| studentId | varchar(20) | NO | MUL | NULL | |
| quizGrade | tinyint(4) | YES | | NULL | |
| quizId | int(11) | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
describe ClassTable2;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| classId | varchar(20) | NO | PRI | NULL | |
| className | varchar(10) | NO | | NULL | |
| classSection | varchar(5) | NO | | NULL | |
| classSemester | varchar(2) | NO | | NULL | |
| classYear | varchar(4) | NO | | NULL | |
| teacherId | varchar(20) | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
describe StudentTable2;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| studentId | varchar(20) | NO | PRI | NULL | |
| lastName | varchar(45) | NO | | NULL | |
| firstName | varchar(45) | NO | | NULL | |
| studentPassword | varchar(32) | NO | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要使用 bactick / 反引号 (`) 字符来引用您的列名称,而不是单引号字符 (')。
此外,您在第一个子查询中将 classId 别名为
Class ID
,但随后在外部where
子句中引用 A.classId。I think you need to use the bactick / backquote (`) character to quote your column names, not the single quote character (').
Also, you are aliasing classId to
Class ID
in the first subquery, but then referencing A.classId in the outerwhere
clause.由于您要从
StudentRecordTable2
和StudentTable2
中进行选择,因此您不能只执行SELECT StudentId
因为该列名称在表和 mysql 中都使用不知道你想要哪一个。您需要指定您想要的表的studentId
,方法是使用表名称作为前缀,就像您稍后在查询中所做的那样。例如,mjla_db.StudentTable2.studentId
。Since you are selecting from both
StudentRecordTable2
andStudentTable2
you can't just do aSELECT studentId
because that column name is used in both tables and mysql doesn't know which one you want. You need to specify which table'sstudentId
you want, by prefixing it with the table name like you have done later in your query.mjla_db.StudentTable2.studentId
for example.select * from (select ...)
是不正确的语法。select * from
后面必须跟表名称。我认为您只需要删除第一个,然后使用括号内的查询即可。我不确定您是否可以在与
AS
一起使用的标识符内使用空格。select * from (select ...)
is incorrect syntax.select * from
must be followed by table name(s). I think you just need to get rid of the first one, and just use the query that is inside the brackets.And I am not sure you can use spaces inside the identifier used with the
AS
.