在mysql中通过传递域名获取域名

vmjh9lq9  于 2021-06-23  发布在  Mysql
关注(0)|答案(4)|浏览(377)

我想通过传递域名来获取域名。
考虑一下,

CREATE TABLE `mails` (
  `idmails` int(11) NOT NULL,
  `mails` varchar(45) DEFAULT NULL
);
INSERT INTO mails
VALUES(1,'harishsng@gmail.com'),
(2,'harish.sn@m-tutor.com'),(3,'harishsn@yahoo.in');

当我通过案例1:harishsng时,结果应该是gmail,案例2:harish.sn应该是m-tutor。
如何在mysql中实现它?

8yoxcaq7

8yoxcaq71#

select mails,substring_index(substring_index(mails,'@',-1),'.',1)
from mails
vxf3dgd4

vxf3dgd42#

SUBSTRING_INDEX 在这里很方便:

SELECT
    idmails,
    mails,
    SUBSTRING_INDEX(SUBSTRING_INDEX(mails, '@', -1), '.', 1) AS domain
FROM mails;

演示

ubof19bj

ubof19bj3#

我想这就是你要找的。您可以使用子字符串索引

select substring_index(substring_index(mails,'.com',1), '@', -1 )  from mails where email like 'harishsng%'
xxslljrj

xxslljrj4#

从bullet.mail中选择replace(replace(replace)(replace(mail,'harishsng',''),'@',''),'.com','');

相关问题