<i id='g9vTZ'><tr id='g9vTZ'><dt id='g9vTZ'><q id='g9vTZ'><span id='g9vTZ'><b id='g9vTZ'><form id='g9vTZ'><ins id='g9vTZ'></ins><ul id='g9vTZ'></ul><sub id='g9vTZ'></sub></form><legend id='g9vTZ'></legend><bdo id='g9vTZ'><pre id='g9vTZ'><center id='g9vTZ'></center></pre></bdo></b><th id='g9vTZ'></th></span></q></dt></tr></i><div id='g9vTZ'><tfoot id='g9vTZ'></tfoot><dl id='g9vTZ'><fieldset id='g9vTZ'></fieldset></dl></div>
        • <bdo id='g9vTZ'></bdo><ul id='g9vTZ'></ul>

        <small id='g9vTZ'></small><noframes id='g9vTZ'>

      1. <tfoot id='g9vTZ'></tfoot>
      2. <legend id='g9vTZ'><style id='g9vTZ'><dir id='g9vTZ'><q id='g9vTZ'></q></dir></style></legend>

        如何在 SQLite 中透视或即选择宽格式存储长格式的表?

        时间:2023-09-18

          <legend id='Aflqd'><style id='Aflqd'><dir id='Aflqd'><q id='Aflqd'></q></dir></style></legend>
                <bdo id='Aflqd'></bdo><ul id='Aflqd'></ul>
              • <small id='Aflqd'></small><noframes id='Aflqd'>

                <i id='Aflqd'><tr id='Aflqd'><dt id='Aflqd'><q id='Aflqd'><span id='Aflqd'><b id='Aflqd'><form id='Aflqd'><ins id='Aflqd'></ins><ul id='Aflqd'></ul><sub id='Aflqd'></sub></form><legend id='Aflqd'></legend><bdo id='Aflqd'><pre id='Aflqd'><center id='Aflqd'></center></pre></bdo></b><th id='Aflqd'></th></span></q></dt></tr></i><div id='Aflqd'><tfoot id='Aflqd'></tfoot><dl id='Aflqd'><fieldset id='Aflqd'></fieldset></dl></div>

                  <tbody id='Aflqd'></tbody>
                  <tfoot id='Aflqd'></tfoot>

                1. 本文介绍了如何在 SQLite 中透视或即选择宽格式存储长格式的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想要一个表格,以长格式和他们在一次查询中获得的所有科目的分数.

                  I'd like to get a table which stores students data in long format and the marks they receive for all of their subjects in one query.

                  这是我的表结构:

                  表格: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
                  

                  我希望结果集具有以下宽格式结构作为结果透视:

                  I want the result set to have the following wide format structure as result of the pivotization:

                  表格:学生信息

                  ## studid ## ## name## ## subjectid_3 ## ## subjectid_4 ## ## subjectid_5 ##
                        A1        Raam        50                60                 70
                        B1        Vivek       60                80                null
                        c1        Alex       null              null                95
                  

                  如何在 SQLite 中完成此操作?

                  How can I accomplish this in SQLite?

                  推荐答案

                  首先需要将当前表改为临时表:

                  First you need to change the current table to a temp table:

                  alter table student_info rename to student_name
                  

                  然后,您需要重新创建student_info:

                  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
                  )
                  

                  然后,填充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 缺少 pivot 函数,所以你能做的最好的事情就是硬编码一些左连接.left join 将匹配其连接条件中的任何行,并为第一个或左表中不满足连接条件的任何行返回 null第二张桌子.

                  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.

                  这篇关于如何在 SQLite 中透视或即选择宽格式存储长格式的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如果不存在则插入 其他更新? 下一篇:在 SQLite 中声明变量并使用它

                  相关文章

                    <tfoot id='tjA1o'></tfoot>

                  1. <i id='tjA1o'><tr id='tjA1o'><dt id='tjA1o'><q id='tjA1o'><span id='tjA1o'><b id='tjA1o'><form id='tjA1o'><ins id='tjA1o'></ins><ul id='tjA1o'></ul><sub id='tjA1o'></sub></form><legend id='tjA1o'></legend><bdo id='tjA1o'><pre id='tjA1o'><center id='tjA1o'></center></pre></bdo></b><th id='tjA1o'></th></span></q></dt></tr></i><div id='tjA1o'><tfoot id='tjA1o'></tfoot><dl id='tjA1o'><fieldset id='tjA1o'></fieldset></dl></div>
                    <legend id='tjA1o'><style id='tjA1o'><dir id='tjA1o'><q id='tjA1o'></q></dir></style></legend>

                    1. <small id='tjA1o'></small><noframes id='tjA1o'>

                        <bdo id='tjA1o'></bdo><ul id='tjA1o'></ul>