em
表
里面的email
字段
通过
substring(参数1,参数2,参数3)函数截取字符串
charindex(参数1,参数2)
函数
查找某个字符的位置
len(字符串)
获得字符串或者字段的长度
先查询字段email的
"@"
==>substring(email,charIndex('@',email)+1,len(email))
charindex的参数分别是
参数1
要查找的字符或者字符串位置
参数2
在哪个字符串或者字段
substring的3个参数分别是
..参数1
要截取的字符串或者字段名-->这里是字段email
参数2
开始的位置,0代表从第一个位置,这里通过charindex获得@的位置,然后+1表示从@后面开始截取,
参数3
结束的位置.通过len(email)
得到
整个字段的长度..
最终的查询语句:
select
*
from
em
order
by
substring(email,charIndex('@',email)+1,len(email)),substring(email,0,charIndex('@',email))
这里order
by
是按
查询后面的2个字段组合排序
================================================
结果
email
id
--------------------------------------------------
-----------
[email protected]5
[email protected]3
[email protected]6
[email protected]1
[email protected]2
[email protected]4
[email protected]7
=================================================
PS:我这里写的是SqlServer的数据库
不同的数据库的
这些函数
一般不会是一样的
你可以查询帮助找到这些
函数的具体使用方法