0

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

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

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

我的查询如下

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

其中 $type 是列名

我使用 codeigniter 作为我的框架

4

1 回答 1

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
于 2013-06-17T06:36:28.840 回答