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

      <small id='23ov6'></small><noframes id='23ov6'>

      1. <tfoot id='23ov6'></tfoot>

      2. <legend id='23ov6'><style id='23ov6'><dir id='23ov6'><q id='23ov6'></q></dir></style></legend>
          <bdo id='23ov6'></bdo><ul id='23ov6'></ul>

        MySQL连接所有列

        时间:2023-06-25

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

              <tfoot id='YmRW9'></tfoot>
                <tbody id='YmRW9'></tbody>
            • <legend id='YmRW9'><style id='YmRW9'><dir id='YmRW9'><q id='YmRW9'></q></dir></style></legend>

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

                • 本文介绍了MySQL连接所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么我们不能在 MySQL 中使用 * 关键字进行连接?

                  Why can we not concatenate in MySQL using the * keyword?

                  SELECT concat(*) FROM table
                  

                  SELECT group_concat(*) FROM table
                  

                  有没有其他方法可以在不显式使用列名的情况下访问列中的值?

                  Is there any other way we could access values in a column without explicitly using the columns name?

                  推荐答案

                  要连接表中的所有列,不能使用 * 关键字,但需要明确列出所有列:

                  To concatenate all columns in a table, you can't use the * keyword, but you need to explicitly list all columns:

                  SELECT CONCAT(col1, col2, col3, ....)
                  FROM yourtable
                  

                  或者您可能想要使用将跳过空值的 CONCAT_WS:

                  or you might want to use CONCAT_WS that will skip null values:

                  SELECT CONCAT_WS(',', col1, col2, col3, ....)
                  FROM yourtable
                  

                  如果您不想手动指定所有列名,您可以使用动态查询.此查询将返回表的所有列名:

                  If you don't want to specify all column names manually, you could use a dinamic query. This query will return all column names of your table:

                  SELECT `column_name` 
                  FROM   `information_schema`.`columns` 
                  WHERE  `table_schema`=DATABASE() 
                         AND `table_name`='yourtable';
                  

                  并使用 GROUP_CONCAT 您可以获得所有列名称的列表:

                  and using GROUP_CONCAT you can obtain a list of all column names:

                  GROUP_CONCAT(CONCAT('`', column_name, '`'))
                  

                  引用,以逗号分隔的格式:

                  quoted, in a comma separated format:

                  `col1`,`col2`,`col3`,`col4`,...
                  

                  所以现在我们拥有了动态创建查询的所有元素:

                  so now we have all the elements to create our query dinamically:

                  SELECT
                    CONCAT(
                      'SELECT CONCAT_WS(\'\',',
                      GROUP_CONCAT(CONCAT('`', column_name, '`') ORDER BY column_name),
                      ') AS all_columns FROM yourtable;')
                  FROM   `information_schema`.`columns` 
                  WHERE  `table_schema`=DATABASE() 
                         AND `table_name`='yourtable'
                  INTO @sql;
                  

                  此查询会将@sql 字符串设置为类似:

                  this query will set the @sql string to something like:

                  SELECT CONCAT_WS('', col1, col2, col3, ....) AS all_columns FROM yourtable
                  

                  这段代码将执行它:

                  PREPARE stmt FROM @sql;
                  EXECUTE stmt;
                  

                  请参阅 fiddle 此处.

                  Please see fiddle here.

                  这篇关于MySQL连接所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 mysql 表名中使用 (-) 破折号 下一篇:当 field=0 时,mySQL 返回所有行

                  相关文章

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

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

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

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