• <bdo id='OjzR9'></bdo><ul id='OjzR9'></ul>
    <legend id='OjzR9'><style id='OjzR9'><dir id='OjzR9'><q id='OjzR9'></q></dir></style></legend>

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

        <tfoot id='OjzR9'></tfoot>
      2. <i id='OjzR9'><tr id='OjzR9'><dt id='OjzR9'><q id='OjzR9'><span id='OjzR9'><b id='OjzR9'><form id='OjzR9'><ins id='OjzR9'></ins><ul id='OjzR9'></ul><sub id='OjzR9'></sub></form><legend id='OjzR9'></legend><bdo id='OjzR9'><pre id='OjzR9'><center id='OjzR9'></center></pre></bdo></b><th id='OjzR9'></th></span></q></dt></tr></i><div id='OjzR9'><tfoot id='OjzR9'></tfoot><dl id='OjzR9'><fieldset id='OjzR9'></fieldset></dl></div>
      3. 添加“字段"从 UNION 到 SQL 结果的表名?

        时间:2023-10-08
        <i id='cbaMz'><tr id='cbaMz'><dt id='cbaMz'><q id='cbaMz'><span id='cbaMz'><b id='cbaMz'><form id='cbaMz'><ins id='cbaMz'></ins><ul id='cbaMz'></ul><sub id='cbaMz'></sub></form><legend id='cbaMz'></legend><bdo id='cbaMz'><pre id='cbaMz'><center id='cbaMz'></center></pre></bdo></b><th id='cbaMz'></th></span></q></dt></tr></i><div id='cbaMz'><tfoot id='cbaMz'></tfoot><dl id='cbaMz'><fieldset id='cbaMz'></fieldset></dl></div>

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

        • <bdo id='cbaMz'></bdo><ul id='cbaMz'></ul>
          <tfoot id='cbaMz'></tfoot>

          1. <legend id='cbaMz'><style id='cbaMz'><dir id='cbaMz'><q id='cbaMz'></q></dir></style></legend>
                  <tbody id='cbaMz'></tbody>

                  本文介绍了添加“字段"从 UNION 到 SQL 结果的表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个简单的联合

                  If I have a simple union

                  select name, phone from users union select name, phone from vendors;
                  

                  有没有办法将表名添加到结果中?

                  Is there a way to add the table name to the results?

                  所以代替

                  +--------+-------+
                  | name   | phone |
                  +--------+-------+
                  | Jim    | 123...|
                  | Macy's | 345...|
                  +--------+-------+
                  

                  我会的

                  +--------+-------+---------+
                  | name   | phone | table   |
                  +--------+-------+---------+
                  | Jim    | 123...| users   |
                  | Macy's | 345...| vendors |
                  +--------+-------+---------+
                  

                  推荐答案

                  select name, phone, 'users' as table_name from users
                  union
                  select name, phone, 'vendors' as table_name from vendors;
                  

                  更好的解决方案是使用union all,这样服务器就不会检查不同的值

                  Better solution will be to use union all, so server will not be checking for distinct values

                  select name, phone, 'users' as table_name from users
                  union all
                  select name, phone, 'vendors' as table_name from vendors;
                  

                  这篇关于添加“字段"从 UNION 到 SQL 结果的表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Select 中合并两个表 (SQL Server 2008) 下一篇:MySQL - 从与常量合并的另一个表中插入数据

                  相关文章

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

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

                    <tfoot id='KmNGV'></tfoot>