Mysql查找列的模式

时间:2013-06-17 06:14:54

标签: php mysql codeigniter

我有这个查询返回列的模式。但是在我的情况下,我需要在表格中返回12列的模式

为每个列运行此查询的效率太低,而且需要很长时间。

有没有办法改进这个查询,以便在一个查询中它返回所有12列的模式?

我的查询如下

SELECT {$type} as mode_value, COUNT({$type}) AS mode 
                                    FROM alterations
                                    GROUP BY   {$type} 
                                    ORDER BY mode DESC 
                                    LIMIT 1

其中$ type是列名

我使用codeigniter作为我的框架

1 个答案:

答案 0 :(得分:0)

你可以尝试:

select modeval, count(*) mode from
(select col1 modeval FROM alterations union all
 select col2 modeval FROM alterations union all
 select col3 modeval FROM alterations union all
 select col4 modeval FROM alterations union all
 select col5 modeval FROM alterations union all
 select col6 modeval FROM alterations union all
 select col7 modeval FROM alterations union all
 select col8 modeval FROM alterations union all
 select col9 modeval FROM alterations union all
 select col10 modeval FROM alterations union all
 select col11 modeval FROM alterations union all
 select col12 modeval FROM alterations) sq
GROUP BY modeval ORDER BY mode DESC LIMIT 1