使用CHAR_LENGTH,例如
SELECT MAX(CHAR_LENGTH(column1)) maxCol1,
MAX(CHAR_LENGTH(column2)) maxCol2,
MAX(CHAR_LENGTH(column3)) maxCol3
FROM tableName
如果列数未知,也可以使用动态SQL.您需要提供的只是数据库的名称和表的名称,
SET @db_Name = 'db_2_21a29';
SET @tb_name = 'TABLENAME';
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('MAX(CHAR_LENGTH(', COLUMN_NAME, ')) AS `', COLUMN_NAME, '`')
) INTO @sql
FROM information_schema.columns
WHERE TABLE_NAME = @tb_name AND
TABLE_SCHEMA = @db_Name;
SET @sql = CONCAT('SELECT ',@sql, 'FROM ', @tb_name);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;