在MySQL中更新命令错误

时间:2017-11-04 22:03:57

标签: mysql sql

我试图根据大学座位号(USN)和主题更新学生的出勤率。 我使用了以下查询,它表示没有任何行受到影响。

Update `Student Attendance` as sa 
JOIN Subjects as s ON sa.`Subject Code`=s.`Subject Code` 
set sa.Attendance ='5' AND sa.`Absent Days`='2017-10-12' 
where sa.USN ='1ks15bt001' AND s.Subject='English';

当我单独运行以下查询时,我将其作为输出:

  Select sa.USN,s.Subject,sa.Attendance
  From `Student Attendance` as sa 
  JOIN Subjects as s ON sa.`Subject Code`=s.`Subject Code` 
  where sa.USN ='1ks15bt001' AND s.Subject='English';

Ouptput

学生表的架构:

CREATE TABLE `Student` (
  `USN` varchar(10) NOT NULL,
  `DOB` date NOT NULL,
  `Dep` varchar(3) NOT NULL,
  `SEM` int(1) NOT NULL,
  `Class` varchar(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `Student`
  ADD PRIMARY KEY (`USN`),
  ADD KEY `Dep` (`Dep`);

学生出勤率:

CREATE TABLE `Student Attendance` (
  `USN` varchar(10) NOT NULL,
  `Subject Code` varchar(6) NOT NULL,
  `Attendance` int(11) NOT NULL DEFAULT '0',
  `Absent Days` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `Student Attendance`
  ADD PRIMARY KEY (`USN`,`Subject Code`),
  ADD KEY `ABC` (`Subject Code`);

主题:

CREATE TABLE `Subjects` (
  `Subject` varchar(40) NOT NULL,
  `Subject Code` varchar(6) NOT NULL,
  `Dep` varchar(3) NOT NULL,
  `Sem` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `Subjects`
  ADD PRIMARY KEY (`Subject Code`,`Subject`) USING BTREE,
  ADD KEY `bgf` (`Dep`);

1 个答案:

答案 0 :(得分:1)

正确的语法是:

Update `Student Attendance` sa join
       Subjects s 
       on sa.`Subject Code` = s.`Subject Code` 
    set sa.Attendance = 5,
        sa.`Absent Days`= '2017-10-12' 
    where sa.USN = '1ks15bt001' AND s.Subject = 'English';

请注意,Attendance是一个数字,因此我删除了单引号。

您的set子句被解析为:

set sa.Attendance = ('5' AND sa.`Absent Days` = '2017-10-12')

这是一个布尔表达式,返回0表示false,1表示true。 0表示相关行有误。