2011-04-23 59 views
0

我正在使用以下语句来查询我的数据库并返回项目a的最常见颜色样式。一旦我得到这些数据,我就想选择最常见的尺寸。选择嵌套的常用属性

select color, style, size 
from orders 
where item = 'item a' 
group by color, style 
order by count(*) desc 
limit 1 

如果按颜色,样式,尺寸分组,它不会计算颜色的数量,样式正确。我怎样才能从数据库中获得最后一条信息?

回答

0

说实话,很难看出一个查询如何比三个查询更有效。但如果你有,你可以将它们合并成一个union一个查询:

select 'Common Color' as Id 
,  (select color from orders where item = 'item a' group by color 
      order by count(*) desc limit 1) as Value 
union all 
select 'Common Style' 
,  (select style from orders where item = 'item a' group by style 
      order by count(*) desc limit 1) 
union all 
select 'Common Size' 
,  (select size from orders where item = 'item a' group by size 
      order by count(*) desc limit 1) 

子查询是否有删除有关羯羊的limit适用于整个工会或只是其中的一部分的任何歧义。

0

使用第二个查询来获得最常见的大小

+0

有没有更高效的方法?我想我可以在同一个声明中这样做。 – Roger 2011-04-23 21:44:42