只是想知道是否有办法检查表格的列到达最大长度。
通过以下我能够获得表格列的长度
SELECT MAX(CHARACTER_MAXIMUM_LENGTH) AS LEN, COLUMN_NAME
FROM information_schema.columns
WHERE table_name IN ('TableA')
GROUP BY COLUMN_NAME
ORDER BY LEN DESC
然后我想用数据检查这些列是否达到MAX长度
下面给出了列的最大长度
SELECT MAX(LENGTH(Col1))
FROM TableA
由于无法在WHERE
子句中使用聚合函数,它是如何实现的?
由于
答案 0 :(得分:2)
您可以使用join
将数据整合在一起:
SELECT MaxLen, ActualLen
FROM (SELECT MAX(CHARACTER_MAXIMUM_LENGTH) AS MaxLen, COLUMN_NAME
FROM information_schema.columns
WHERE table_name IN ('TableA') AND COLUMN_NAME = 'COL1'
) m CROSS JOIN
(SELECT MAX(LENGTH(Col1)) as ActualLen
FROM TableA
) c;
我不确定你真正想要的是什么。如果您希望行具有最大值:
select a.*
from tablea a
where length(col1) = (SELECT MAX(CHARACTER_MAXIMUM_LENGTH) AS MaxLen
FROM information_schema.columns
WHERE table_name IN ('TableA') AND COLUMN_NAME = 'COL1'
);