mysql unique count_MySQL - Count Number of Unique Values

问题

If I have three columns:

orderNumber, name, email

and I would like to count how many unique emails are in the table how would I go about doing so?

A statement like:

SELECT count(email) FROM orders

gives me the total count.

I tried SELECT DISTINCT count(email) FROM orders

but that does not seem to be giving me the numbers I am expecting.

回答1:

use

SELECT count( DISTINCT(email) ) FROM orders

Distinct provide unique email ids and then simply count them.

回答2:

SELECT count(DISTINCT(email)) FROM orders

its different from your posting, since its filters out the duplicates before counting it

回答3:

For best performance you should use:

SELECT

sub.email,

count(1) as 'count_unique'

FROM

(SELECT email FROM orders GROUP by email) sub

回答4:

The accepted soultion doesn't work for me - it returns a "1" for each unique email address in the table.

This is what I had to do to get the info I needed:

select email, count(email) AS total from sysAccessLog group by email order by total desc

Which returns a list of email addresses and the number of occurrences.

来源:https://stackoverflow.com/questions/16697215/mysql-count-number-of-unique-values

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值