mysql 统计表中的邮箱用户数量

create table studs(
id int,
name varchar(30),
mail varchar(50)
);
insert into studs values(1,'Jack','[email protected]');
insert into studs values(2,'Mary','[email protected]');
insert into studs values(3,'Alex','[email protected]');
insert into studs values(4,'Jerry','[email protected]');
insert into studs values(5,'Jack','[email protected]');
insert into studs values(6,'Jack','[email protected]');
insert into studs values(7,'Jack','[email protected]');
insert into studs values(8,'Jack','[email protected]');



drop FUNCTION IF EXISTS subemail;

CREATE FUNCTION subemail(in_email VARCHAR(255)) RETURNS VARCHAR(60)
BEGIN
	DECLARE i INT DEFAULT 1;
		set i = locate('@',in_email);
		set in_email = SUBSTR(in_email,i);
	return in_email;
end;

SELECT t.email,count(1) from (SELECT subemail(mail) as email from studs) t GROUP BY t.email;


mysql 统计表中的邮箱用户数量_第1张图片

你可能感兴趣的:(mysql 统计表中的邮箱用户数量)