简体   繁体   English

如何选择表中包含最长字符串信息的文本列

[英]How to select textual columns of a table with the information about the longest string in them

I am selecting columns like this: 我正在选择像这样的列:

    SELECT table_name, column_name
    FROM information_schema.`COLUMNS` 
    WHERE table_schema ='db_name' 
      AND data_type IN ('varchar', 'text') 
      AND table_name='table_name';

The result is a table name and column name on each row. 结果是每一行的表名和列名。 In the third column I would like to see a number of bytes of the longest string in that column of the table. 在第三列中,我希望在表的该列中看到最长字符串的多个字节。

I am trying to optimize a table becouse when I am trying to convert data from latin2 to utf8 I get the "Row size too large." 当我尝试将数据从latin2转换为utf8时,我试图优化表,因为“行大小太大”。 error. 错误。 So I want to change varchar (255) to lower number if possible. 所以我想尽可能将varchar(255)更改为较小的数字。 I would like to avoid changing columns to TEXT if it will be possible. 如果可能,我想避免将列更改为TEXT。

The mysql function you are looking for is the length() function http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length 您要查找的mysql函数是length()函数http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length

Combined with max(length(COLUMN_NAME)) and a group by statement, you should be fine. max(length(COLUMN_NAME))group by语句结合使用,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM