11

这里还有其他听起来相似但不是的问题。我有一个查询,它返回一堆带有 group by 的行,我想对总 group by 行应用限制,而不是用于创建组的总行数。

ID    TYPE        COLOR        SIZE
----------------------------------------
1     Circle      Blue         Large
2     Circle      Red          Large
3     Square      Green        Large
4     Circle      Purple       Large
5     Circle      Blue         Small
6     Circle      Yellow       Medium
7     Circle      Black        Large
8     Oval        Blue         Large
9     Circle      Gray         Small
10    Triangle    Black        Large
11    Star        Green        Large
12    Triangle    Purple       Large

SELECT size, type FROM clothes WHERE size = 'large' GROUP BY type LIMIT 0, 5

TYPE       SIZE       ROWS
---------------------------    
Circle     Large      4
Square     Large      1

^^^^ 2 组按行已经用尽了我的限制

TYPE       SIZE       ROWS
---------------------------    
Circle     Large      4
Square     Large      1
Oval       Large      1
Triangle   Large      2
Star       Large      1

^^^^ 这是我想要的,限制适用于组

必须有一些子查询或我可以在这里做的事情,但我没有弄清楚。

谢谢。

4

3 回答 3

7

这对我有用:

SELECT type, size, COUNT(*) AS rows
FROM clothes
WHERE size = 'large'
GROUP BY type
LIMIT 0, 5

结果是:

type      size   rows
------------------------
Circle    Large     4
Oval      Large     1
Square    Large     1
Star      Large     1
Triangle  Large     2

LIMIT 应该在 GROUP BY 之后应用,所以我不明白这个问题。

于 2012-06-16T00:25:23.953 回答
3
SELECT * FROM (
  SELECT id, color, size, type FROM clothes WHERE size = 'large' GROUP BY type 
) AS baseview LIMIT 0, 25
于 2012-06-15T22:33:14.403 回答
0

数数有多少怎么样?

select color, size, type, count(id) from clothes where size = 'large' group by size, type, color;

我是 sql 编程的新手,但这应该返回以下内容,这样您就可以为每种颜色/尺寸/类型组合使用 1 行并在其后获得数量计数。

红色大衬衫4

绿色大号衬衫6

绿色大裤2

等等...

于 2012-06-15T22:34:36.240 回答