MySQL 截断 GROUP_CONCAT 函数的连接结果

2023-11-28数据库问题
28

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

问题描述

我创建了一个视图,该视图使用 GROUP_CONCAT 将产品列查询的结果与数据类型 'varchar(7) utf8_general_ci' 连接到名为的列中concat_products.

I've created a view which uses GROUP_CONCAT to concatenate results from a query on products column with data type of 'varchar(7) utf8_general_ci' in a column named concat_products.

问题是 MySQL 截断了concat_products"的值;柱子.phpMyAdmin 表示concat_products"的数据类型;列是 varchar(341) utf8_bin

The problem is that MySQL truncates value of "concat_products" column. phpMyAdmin says the data type of "concat_products" column is varchar(341) utf8_bin

餐桌产品:

CREATE TABLE `products`(
    `productId` tinyint(2) unsigned NOT NULL AUTO_INCREMENT, 
    `product` varchar(7) COLLATE utf8_general_ci NOT NULL, 
    `price` mediumint(5) unsigned NOT NULL, 
    PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci

concat_products_vw"视图:

The "concat_products_vw" view:

CREATE VIEW concat_products_vw AS
SELECT
  `userId`,
    GROUP_CONCAT(CONCAT_WS('_', `product`, `productId`, `price`) 
        ORDER BY `productId` ASC SEPARATOR '*') AS concat_products
FROM
  `users`
LEFT JOIN `products` 
ON `users`.`accountBalance` >= `product`.`price`
GROUP BY `productId` 

根据 MySQL 手册:

According to MySQL manual:

VARCHAR 列中的值是可变长度的字符串
在 MySQL 4.0.2 之前,长度可以指定为 1 到 255 之间的值,MySQL 4.0.2 之前可以指定为 0 到 255.

Values in VARCHAR columns are variable-length strings
Length can be specified as a value from 1 to 255 before MySQL 4.0.2 and 0 to 255 as of MySQL 4.0.2.


编辑

VARCHAR 列中的值是可变长度的字符串.长度可以指定为 0 到 65,535 之间的值.

  1. 为什么 MySQL 为 varchar concat_products"指定了超过 255 个字符?柱子?(解决了!)

  1. Why MySQL specifies more than 255 characters for varchar "concat_products" column? (solved!)

为什么是 uf8_bin 而不是 utf8_general_ci?

Why uf8_bin instead of utf8_general_ci?

是否可以将视图中列的数据类型更改为concat_products"的文本,例如在我的情况下?列?

Is it possible to change the data type of a column in a view for example in my case to text for "concat_products" column?

如果不是,我能做些什么来防止 MySQL 截断concat_products";列?

If not what can I do to prevent MySQL from truncating "concat_products" column?

推荐答案

正如我在之前的评论中所写,MySQL 手册 说:

As I already wrote in an earlier comment, the MySQL manual says:

VARCHAR 列中的值是可变长度的字符串.长度可以指定为 0 到 65,535 之间的值.

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535.

所以问题不在于字段的数据类型.

So the problem is not with the data type of the field.

MySQL 手册也说:

结果被截断为由group_concat_max_len 系统变量,默认值为1024. 该值可以设置得更高,虽然返回值的有效最大长度受值的限制max_allowed_packet.更改值的语法group_concat_max_len 在运行时如下,其中 val 是一个无符号整数:设置 [全球 |SESSION] group_concat_max_len = val;

The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer: SET [GLOBAL | SESSION] group_concat_max_len = val;

更改 group_concat_max_len 值的选项是:

Your options for changing the value of group_concat_max_len are:

  1. 通过将以下内容附加到命令来更改 MySQL 启动时的值:
    --group_concat_max_len=your_value_here
  2. 在您的 MySQL 配置文件 (mysql.ini) 中添加这一行:group_concat_max_len=your_value_here
  3. 在 MySQL 启动后运行此命令:
    SET GLOBAL group_concat_max_len=your_value_here;
  4. 在打开 MySQL 连接后运行此命令:
    SET SESSION group_concat_max_len=your_value_here;

文档:SET、服务器系统变量:group_concat_max_len

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

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14