在 MySQL 中通过传递名称获取域名?

mysqlmysqli database更新于 2025/9/3 3:24:17

要在 MySQL 中通过传递名称获取域名,可以使用 substring_index()。让我们首先创建一个表 −

mysql> create table DemoTable
   (
   UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   UserMailId varchar(200)
   );
Query OK, 0 rows affected (0.77 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable(UserMailId) values('John9989@facebook.com');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(UserMailId) values('983773CS@yahoo.com');
Query OK, 1 row affected (0.23 sec)

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

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

这将产生以下输出 −

+--------+-----------------------+
| UserId | UserMailId            |
+--------+-----------------------+
|      1 | John9989@facebook.com |
|      2 | 983773CS@yahoo.com    |
|      3 | Chris95@gmail.com     |
+--------+-----------------------+
3 rows in set (0.00 sec)

以下是在 MySQL 中通过传递名称来获取域名的查询。

mysql> select
   UserId,UserMailId,
   substring_index(substring_index(UserMailId, '@', -1), '.', 1) AS `Domain_Name`
   from DemoTable;

这将产生以下输出。此处获取域名 −

+--------+-----------------------+-------------+
| UserId | UserMailId            | Domain_Name |
+--------+-----------------------+-------------+
|      1 | John9989@facebook.com | facebook    |
|      2 | 983773CS@yahoo.com    | yahoo       |
|      3 | Chris95@gmail.com     | gmail       |
+--------+-----------------------+-------------+
3 rows in set (0.01 sec)

相关文章


有用资源