数据库
首页 > 数据库> > group_concat上的MySQL varchar列在View中显示为文本

group_concat上的MySQL varchar列在View中显示为文本

作者:互联网

我在基本表中的列为acct_num varchar(25),并且正在创建一个View并将GROUP_CONCAT()设置为此列.在show create视图中以文本数据类型显示. GROUP_CONCAT(acct_num)列是否可以具有VARCHAR(25)数据类型.请指教.

我从MySQL参考中学到了什么:

  • I though to cast as varchar(25) but CAST can be applied as CHAR not as VARCHAR
  • There is option to set GLOBAL_SET_GROUP_CONCAT_VALUE = 512 so that you can get output of GROUP_CONCAT() as varchar() – but it didn’t work
    out for me.

解决方法:

您可以设置group_concat_max_len来实现.

这是一个演示:

SQL:

-- To change the setting globally
set global group_concat_max_len = 512;
-- To change the setting only for current session
set group_concat_max_len = 512;
create table t1(acct_num varchar(25));
create view v1 as select group_concat(acct_num) as gc_acct_num from t1;
desc v1;

输出:

mysql> -- To change the setting globally
mysql> set global group_concat_max_len = 512;
Query OK, 0 rows affected (0.00 sec)

mysql> -- To change the setting only for current session
mysql> set group_concat_max_len = 512;
Query OK, 0 rows affected (0.00 sec)

mysql> create table t1(acct_num varchar(25));
Query OK, 0 rows affected (0.01 sec)

mysql> create view v1 as select group_concat(acct_num) as gc_acct_num from t1;
Query OK, 0 rows affected (0.00 sec)

mysql> desc v1;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| gc_acct_num | varchar(512) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)

标签:group-concat,mysql
来源: https://codeday.me/bug/20191118/2031729.html