2011-04-23 46 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