Mysql子字符串 [英] Mysql substring

查看:157
本文介绍了Mysql子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有我试图迁移到一个新的邮件服务器,所以我想写Mysql脚本返回一个表如下
然后导出结果作为CSV文件
sql语句如下

Hi all i'm trying to migrate to a new mail server so i want to wrote Mysql script to return a table as the following then export the result as CSV file sql statement as the following

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

我想在@和@符号前面的任何字符子字符串之前的任何想法将是伟大的协助

i want to substring any characters preceding the @ and the @ symbol iteself before the domain name any ideas would be great assist

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

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


推荐答案

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

You can use LOCATE to find the position of the @:

 LOCATE('@',email)

因此要查找域:

SELECT CASE 
    WHEN LOCATE('@',email) = 0 THEN ''
    ELSE SUBSTRING(email,LOCATE('@',email)+1)
    END as Domain
FROM YourTable

这篇关于Mysql子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆