5

大家好,我正在尝试迁移到新的邮件服务器,所以我想编写 Mysql 脚本以返回如下表,然后将结果导出为 CSV 文件 sql 语句,如下所示

`select email,clear,email AS domain from postfix_users `

我想在域名之前对@和@符号本身之前的任何字符进行子串化,任何想法都会有很大帮助

mysql> select email,clear,email AS domain from postfix_users ;

+---------------------------+--------+---------------------------+
| email                     | clear  | domain                    |
+---------------------------+--------+---------------------------+
| user@domain.tld           | passw  | user@domain.tld           |
+---------------------------+--------+---------------------------+
4

1 回答 1

14

您可以使用 LOCATE 来查找 @ 的位置:

 LOCATE('@',email)

所以要找到域:

SELECT CASE 
    WHEN LOCATE('@',email) = 0 THEN ''
    ELSE SUBSTRING(email,LOCATE('@',email)+1)
    END as Domain
FROM YourTable
于 2009-06-14T17:00:24.070 回答