问题描述
为什么我们不能在 MySQL 中使用 * 关键字进行连接?
Why can we not concatenate in MySQL using the * keyword?
或
有没有其他方法可以在不显式使用列名的情况下访问列中的值?
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:
或者您可能想要使用将跳过空值的 CONCAT_WS
:
or you might want to use CONCAT_WS
that will skip null values:
如果您不想手动指定所有列名,您可以使用动态查询.此查询将返回表的所有列名:
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:
并使用 GROUP_CONCAT 您可以获得所有列名称的列表:
and using GROUP_CONCAT you can obtain a list of all column names:
引用,以逗号分隔的格式:
quoted, in a comma separated format:
所以现在我们拥有了动态创建查询的所有元素:
so now we have all the elements to create our query dinamically:
此查询会将@sql 字符串设置为类似:
this query will set the @sql string to something like:
这段代码将执行它:
请参阅 fiddle 此处.
Please see fiddle here.
这篇关于MySQL连接所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!