SQL查找列中不同值的数量 [英] SQL to find the number of distinct values in a column

查看:353
本文介绍了SQL查找列中不同值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下方式选择列中的所有不同值:

I can select all the distinct values in a column in the following ways:


  • SELECT DISTINCT column_name FROM table_name;

  • 从表名GROUP BY column_name中选择column_name;

  • SELECT DISTINCT column_name FROM table_name;
  • SELECT column_name FROM table_name GROUP BY column_name;

但是如何从该查询中获取行数?

But how do I get the row count from that query? Is a subquery required?

推荐答案

您可以在其中使用 DISTINCT 关键字 COUNT 聚合函数:

You can use the DISTINCT keyword within the COUNT aggregate function:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

这将仅计算该列的不同值。

This will count only the distinct values for that column.

这篇关于SQL查找列中不同值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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