<bdo id='Uhjjl'></bdo><ul id='Uhjjl'></ul>
    1. <small id='Uhjjl'></small><noframes id='Uhjjl'>

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

      MySQL:如何多次加入同一个表?

      MySQL: How do I join same table multiple times?(MySQL:如何多次加入同一个表?)

      <small id='3IY8f'></small><noframes id='3IY8f'>

        <tbody id='3IY8f'></tbody>

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

              1. 本文介绍了MySQL:如何多次加入同一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有两个表 ticketattr.表 ticketticked_id 字段和其他几个字段.表 attr 有 3 个字段:

                I have two tables ticket and attr. Table ticket has ticked_id field and several other fields. Table attr has 3 fields:

                ticket_id - numeric
                attr_type - numeric
                attr_val - string
                

                attr_type 是一个固定的枚举值.例如,它可以是 123.

                attr_type is a fixed enum of values. For example, it can be 1, 2 or 3.

                我需要进行查询,查询结果将是 4 列:

                I need to make a query, the result of which will be 4 columns:

                ticket_idattr_val 用于attr_type=1attr_val 用于attr_type=2>, attr_val for attr_type=3

                ticket_id, attr_val for attr_type=1, attr_val for attr_type=2, attr_val for attr_type=3

                如果attr表中没有attr_type对应的值,则对应列中应显示NULL值.

                If there is no corresponding value for attr_type in attr table then NULL value should be shown in corresponding column.

                示例:

                ticket
                    ticket_id: 1
                
                    ticket_id: 2
                
                    ticket_id: 3
                
                
                attr
                    ticket_id: 1
                    attr_type: 1
                    attr_val: Foo
                
                    ticket_id: 1
                    attr_type: 2
                    attr_val: Bar
                
                    ticket_id: 1
                    attr_type: 3
                    attr_val: Egg
                
                    ticket_id: 2
                    attr_type: 2
                    attr_val: Spam
                

                结果应该是:

                ticked_id: 1
                attr_val1: Foo
                attr_val2: Bar
                attr_val3: Egg
                
                ticked_id: 2
                attr_val1: NULL
                attr_val2: Spam
                attr_val3: NULL
                
                ticked_id: 3
                attr_val1: NULL
                attr_val2: NULL
                attr_val3: NULL
                

                我尝试了 3 次左加入 attr 表,但无法弄清楚如何通过 attr_type

                I tried left joining attr table 3 times, but cannot figure out how to arrange output by attr_type

                推荐答案

                你需要使用多个LEFT JOINs:

                SELECT 
                    ticket.ticket_id,  
                    a1.attr_val AS attr_val1,
                    a2.attr_val AS attr_val2,
                    a3.attr_val AS attr_val3
                FROM ticket
                    LEFT JOIN attr a1 ON ticket.ticket_id=a1.ticket_id AND a1.attr_type=1
                    LEFT JOIN attr a2 ON ticket.ticket_id=a2.ticket_id AND a2.attr_type=2
                    LEFT JOIN attr a3 ON ticket.ticket_id=a3.ticket_id AND a3.attr_type=3
                

                这是一个例子:SQL Fiddle.

                这篇关于MySQL:如何多次加入同一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Bogus foreign key constraint fail(虚假外键约束失败)
                how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)
              2. <tfoot id='Pt47V'></tfoot>
                  1. <small id='Pt47V'></small><noframes id='Pt47V'>

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

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

                          <tbody id='Pt47V'></tbody>