mysql - 无法运行查询

标签 mysql sql mysql-error-1242

我必须根据下表找到结果

学生们 StudentPapersSelection 作为 sps StudentGroupManagemt 作为 sgm 内部数据作为 iars

from student 我需要students rollno 和name 其中iars 的paperid = sps 的paperid 和iars groupid = sgm group id 和students id 应该基于前两个。

我正在运行的查询是:

select students.rollno, students.name 
from students,sps,iars,sgm 
where iars.id=1
and students.studentid=(select studentid 
                        from sps where sps.paperid=iars.paperid
                        and iars.id=1)
and students.studentid=(select studentid 
                        from sgm 
                        where sgm.groupid=iars.groupid 
                        and iars.id=1) 
and students.course=iars.courseid  
and students.semester=iars.semester

它表示查询返回多于 1 行。我讨厌这个问题。

最佳答案

我会试试看:

select  students.rollno,
        students.name
from    iars, students join sps on students.studentid = sps.studentid
        join sgm on students.studentid = sgm.studentid
where   iars.id = 1 
and     sps.paperid=iars.paperid
and     sgm.groupid=iars.groupid
and     students.course = iars.courseid
and     students.semester = iars.semester

假设这样的表:

CREATE TABLE `students` (
  `studentid` int(11) NOT NULL AUTO_INCREMENT,
  `rollno` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `course` int(11) DEFAULT NULL,
  `semester` int(11) DEFAULT NULL,
  PRIMARY KEY (`studentid`)
) ENGINE=InnoDB AUTO_INCREMENT=66820 DEFAULT CHARSET=latin1


CREATE TABLE `sps` (
  `studentid` int(11) NOT NULL AUTO_INCREMENT,
  `paperid` int(11) DEFAULT NULL,
  PRIMARY KEY (`studentid`)
) ENGINE=InnoDB AUTO_INCREMENT=66820 DEFAULT CHARSET=latin1


CREATE TABLE `sgm` (
  `studentid` int(11) NOT NULL AUTO_INCREMENT,
  `groupid` int(11) DEFAULT NULL,
  PRIMARY KEY (`studentid`)
) ENGINE=InnoDB AUTO_INCREMENT=66820 DEFAULT CHARSET=latin1


CREATE TABLE `iars` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `paperid` int(11) DEFAULT NULL,
  `groupid` int(11) DEFAULT NULL,
  `courseid` int(11) DEFAULT NULL,
  `semester` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=66820 DEFAULT CHARSET=latin1

像这样的数据:

insert into students values (1,1,'a',1,1);
insert into students values (2,1,'b',1,1);
insert into iars values(1,1,1,1,1);
insert into sgm values (1,1);
insert into sps values (1,1);

关于mysql - 无法运行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11603315/

相关文章:

mysql - 如何在mysql中获取毫秒数

SQL SELECT multiple with sum

sql - 错误: syntax error at or near "select"

mysql - 子查询在mysql中返回多于1行

sql - 无法绕过 mysql 子查询

mysql - 为什么如果我在 WHERE 子句中再添加一个条件,那么使用索引就会失败?

c# - 在 ASP.NET 中使用 MySqlCommand 时出现 MySQL 语法错误

java - 当我通过命令提示符运行我的 java 文件时出现错误

android - SQLite 查询 : selecting the newest row for each distinct contact

mysql - 在 INSERT INTO 语句 MySQL 中引用当前行