替换字符串中的特定char

Dec*_*ine 3 mysql sql

我有一个名为表Attendance和列叫attendanceStatusCourseID.该attendanceStatus列是文本类型,因为我希望能够在任何给定阶段将每个更改0为a 1.数据用0's 填充,但我希望0将字符串中的最后一个更改为a 1.数据包含18个0,所以它将是我需要改变的第18个字符.

我觉得我已经接近这一点,但如果我不亲近,我愿意听取它完全改变.

UPDATE Attendance
SET attendanceStatus REPLACE = (attendanceStatus, '0', '1')
WHERE CourseID like '2%';
Run Code Online (Sandbox Code Playgroud)

我意识到这个代码将所有的0改为1,我只希望改变第18个0.

Gor*_*off 8

您正在使用错误的方法存储出勤状态.虽然我可以理解为什么你会把它们存放在一个字符串中,但它真的很麻烦.你怎么回答:有多少学生参加了至少10天?第6天有多少学生?有多少学生连续三天缺席?

正确的方法是每个"日"都有一个单独的行(我不确定正确的单位是什么).它会是这样的:

create table StudentAttendance (
    StudentAttendanceId int not null primary key auto_increment,
    StudentId int not null references Student(StudentId),
    CourseId int not null references Courses(CourseId),
    Date date,
    status char(1),
    constraint unq_scd unique (StudentId, CourseId, Date)
);
Run Code Online (Sandbox Code Playgroud)