数据透视SQLite
发布时间:2020-12-12 19:16:10 所属栏目:百科 来源:网络整理
导读:我想在一个查询中得到一张表,显示学生和他们所有科目的标记。 这是我的表结构: 表:markdetails ## studid ## ## subjectid ## ## marks ## A1 3 50 A1 4 60 A1 5 70 B1 3 60 B1 4 80 C1 5 95 表:学生信息 实际结构: ## studid ## ## name ## A1 Raam B1
我想在一个查询中得到一张表,显示学生和他们所有科目的标记。
这是我的表结构: 表:markdetails ## studid ## ## subjectid ## ## marks ## A1 3 50 A1 4 60 A1 5 70 B1 3 60 B1 4 80 C1 5 95 表:学生信息 实际结构: ## studid ## ## name ## A1 Raam B1 Vivek c1 Alex 我想结果集看起来像这样: 表:学生资料 ## studid ## ## name## ## subjectid_3 ## ## subjectid_4 ## ## subjectid_5 ## A1 Raam 50 60 70 B1 Vivek 60 80 null c1 Alex null null 95 我如何在SQLite中完成这个? 首先,您需要将当前表更改为临时表:alter table student_info rename to student_name 然后,您将要重新创建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 ) 然后,填充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 现在,只要放你的临时表: drop table student_temp 这就是您如何快速更新您的表。 SQLite缺少一个枢纽功能,所以你可以做的最好是硬编码一些左连接。左连接将匹配其连接条件中的任何行,并从不符合第二个表的连接条件的第一个或左侧表中的任何行返回null。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |