MySQL - 按标准分组并计算分组在一起的项目数量 - 如何? [英] MySQL - group by criterion AND count # of items that are grouped together - how?

查看:62
本文介绍了MySQL - 按标准分组并计算分组在一起的项目数量 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法执行 1 个查询来选择所有项目(SELECT * FROM t-shirts),按特定条件将它们分组(GROUP BY style", color") 但同时计算组合在一起的 UNIQUE 'color' 项目的数量?我可以通过循环遍历每个 style->color 并计算项目数量来做到这一点,但是我想也许有一种更简单的方法可以做到这一点.

Is there a way to execute 1 query that would select all items ("SELECT * FROM t-shirts), group them by certain criterion ("GROUP BY style, color") but at the same time count the # of UNIQUE 'color' items that were grouped together? I can do that by cycling through each style->color and count the number of items, but I thought that perhaps there's an easier way of doing that.

谢谢.

附言解决:从 GROUP BY 中删除 color,并使用COUNT(distinct color)":

P.S. Solved: remove color from GROUP BY, and used "COUNT (distinct color)":

SELECT *, COUNT (distinct color) FROM t-shirts GROUP BY style

推荐答案

尝试SELECT style, COUNT(*) FROM t-shirts GROUP BY style.

这篇关于MySQL - 按标准分组并计算分组在一起的项目数量 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆