從電子郵件 ID 中獲取域名的 MySQL 查詢?


為此使用 SUBSTRING_INDEX()。讓我們首先建立一個表 -

mysql> create table DemoTable
-> (
-> UserMailId varchar(100)
-> );
Query OK, 0 rows affected (0.68 sec)

使用插入命令在表中插入一些記錄 -

mysql> insert into DemoTable values('John@gmail.com');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('Carol94844@yahoo.com');
Query OK, 1 row affected (0.25 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

結果

這將產生以下結果 -

+----------------------+
| UserMailId           |
+----------------------+
| John@gmail.com       |
| Carol94844@yahoo.com |
+----------------------+
2 rows in set (0.00 sec)

以下是獲取域名名的查詢 -

mysql> select substring_index(UserMailId,'@',-1) as RightSideValue from DemoTable;

結果

這將產生以下結果 -

+----------------+
| RightSideValue |
+----------------+
| gmail.com      |
| yahoo.com      |
+----------------+
2 rows in set (0.00 sec)

更新時間: 30-6 月 -2020

631 次觀看

開啟您的 職業生涯

完成該課程即可獲得認證

開始學習
廣告