select t.table_name from information_schema.tables t
left join information_schema.columns c
on c.table_name = t.table_name
and c.table_schema = t.table_schema
where t.table_schema = 'test'
and count(c.column_name) = max(count(c.column_name))
我正在尝试从数据库“test”中选择具有最高列数的表。但是,我无法做到这一点。我也尝试选择count(c.column_name),但不是返回每个表的列数,而是返回最大列数。
如何选择列数最多的表?
答案 0 :(得分:2)
尝试按表名分组。类似的东西:
SELECT TABLE_NAME, COUNT(*) as c
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
ORDER BY c desc
LIMIT 1;
答案 1 :(得分:1)
试试这个
select table_name, col_count
from
(
select t.table_name, count(c.column_name) as col_count
from information_schema.tables t
left join information_schema.columns c
on c.table_name = t.table_name
and c.table_schema = t.table_schema
where t.table_schema = 'test'
group by t.table_name
) r
order by col_count desc
答案 2 :(得分:1)
有点重复Mike的答案,但我还要添加数据库名称:
SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(TABLE_NAME) AS C
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_SCHEMA, TABLE_NAME
ORDER BY C DESC
LIMIT 0, 1