• <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 结果的表名?

        Add quot;fieldquot; of table name to SQL results from UNION?(添加“字段从 UNION 到 SQL 结果的表名?)
        <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 结果的表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

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

                    <tbody id='KmNGV'></tbody>
                  <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>