• <bdo id='Gh7Mp'></bdo><ul id='Gh7Mp'></ul>

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

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

    <legend id='Gh7Mp'><style id='Gh7Mp'><dir id='Gh7Mp'><q id='Gh7Mp'></q></dir></style></legend>

        将两个垂直表合并到一个水平表

        时间:2023-08-20

          <tbody id='i1sFx'></tbody>

          <bdo id='i1sFx'></bdo><ul id='i1sFx'></ul>
          <tfoot id='i1sFx'></tfoot>

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

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

                <legend id='i1sFx'><style id='i1sFx'><dir id='i1sFx'><q id='i1sFx'></q></dir></style></legend>

                  本文介绍了将两个垂直表合并到一个水平表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  表格定义

                  Table 1 (horizo​​ntal) 这是一个用户表

                  Table 1 (horizontal) This is a table of users

                  | id | name | phone |
                  ---------------------
                  | 1  | Bob  | 800   |
                  | 2  | Phil | 800   | 
                  

                  Table 2 (Vertical Table) 这是一个团队表

                  Table 2 (Vertical Table) This is a table of teams

                  | id | name      |
                  ------------------
                  | 1  | Donkey    |
                  | 2  | Cat       |  
                  

                  Table 3 (Vertical Table) 此表连接前两个

                  Table 3 (Vertical Table) This table is connecting the first two

                  | id | user_id | team_id |
                  --------------------------
                  | 1  |    1    |   1     |
                  | 2  |    1    |   2     |
                  | 3  |    2    |   1     |
                  

                  <小时>

                  我的目标

                  我希望能够以返回以下信息的方式查询数据:

                  I would like to be able to query the data in such a way that i get the following back:

                  | id | name | phone | Donkey | Cat  |
                  -------------------------------------
                  | 1  | Bob  | 800   | 1      | 1    |
                  | 2  | Phil | 800   | 1      | Null |
                  

                  此表将包含我的水平表数据,然后是其他两个垂直表的组合以创建附加列.表 2 最终成为列名标题.并且行值作为布尔值从表三中提取.

                  This table would have my horizontal table data, then a combination of the other two vertical tables to create the appended columns. Where table 2 ends up being the column name headings. And the row valus are pulled from table three as a boolean.

                  推荐答案

                  您正在寻找数据透视表:

                  You're chasing a pivot table:

                  select u.*, 
                    sum(case when t1.name = 'Donkey' then 1 else 0 end) Donkey, 
                    sum(case when t1.name = 'Cat' then 1 else 0 end) Cat
                    from users u
                      inner join user_team ut1
                        on u.id = ut1.user_id  
                      inner join teams t1
                        on ut1.team_id = t1.id
                    group by name
                  

                  演示:http://sqlfiddle.com/#!9/5fd33/7

                  这篇关于将两个垂直表合并到一个水平表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:mysql 选择更新 下一篇:在 m1 处理器中运行的 Docker 镜像

                  相关文章

                  最新文章

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

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

                    2. <tfoot id='nODcb'></tfoot>