MySql获取不同域的计数

时间:2017-09-21 06:18:24

标签: php mysql mariadb

我有这样的用户表。用户可以通过不同的电子邮件进行注册,例如abc@xyz.comabc@uvw.com。我需要从电子邮件中获取不同域名的数量。我的users表结构是

+----+------------------+------------------------+
| id | name             | email                  |
+----+------------------+------------------------+
|  3 | shamsreza        | shamsreza@abc.in       |
|  4 | Kamal            | kamalkumar@xyz.com     |
|  6 | BIBHUDATTA SAHOO | admin@uvw.com          |
|  7 | Bibhu Once Again | bibhuduttasahoo@xyz.in |
+----+------------------+------------------------+  

我这样查询

select substring(email,LOCATE('@',email),LENGTH (email)) from users

它给我输出

+---------------------------------------------------+
| substring(email,LOCATE('@',email),LENGTH (email)) |
+---------------------------------------------------+
| @uvw.com                                          |
| @xyz.com                                          |
| @xyz.com                                          |
| @abc.in                                           |
+---------------------------------------------------+

但是这里重复了xyz.com,我需要计算不同的域名。

1 个答案:

答案 0 :(得分:1)

获取不同的结果,您可以使用DISTINCT。

select distinct substring(email,LOCATE('@',email),LENGTH (email)) from users

或者,如果您想要计算它们:

select count(distinct substring(email,LOCATE('@',email),LENGTH (email))) from users