First you need to change the current table to a temp table:
alter table student_info rename to student_name
Then, you'll want to recreate student_info
:
create table student_info add column (
stuid VARCHAR(5) PRIMARY KEY,
name VARCHAR(255),
subjectid_3 INTEGER,
subjectid_4 INTEGER,
subjectid_5 INTEGER
)
Then, populate student_info
:
insert into student_info
select
u.stuid,
u.name,
s3.marks as subjectid_3,
s4.marks as subjectid_4,
s5.marks as subjectid_5
from
student_temp u
left outer join markdetails s3 on
u.stuid = s3.stuid
and s3.subjectid = 3
left outer join markdetails s4 on
u.stuid = s4.stuid
and s4.subjectid = 4
left outer join markdetails s5 on
u.stuid = s5.stuid
and s5.subjectid = 5
Now, just drop your temp table:
drop table student_temp
And that's how you can quickly update your table.
SQLite lacks a pivot
function, so the best you can do is hard-code some left joins. A left join
will bring match any rows in its join conditions and return null
for any rows from the first, or left, table that don't meet the join conditions for the second table.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…