现在有表结构如下:
users表:
id | name | mobile |
---|---|---|
1 | 张三 | 18112345678 |
2 | 李四 | 18212345678 |
. | ... | ... |
n | ... | ... |
现在想查询到所有符合181号段的手机号,有下面几种方式可行:
select mobile from users where substring(mobile,1,3)='181';
select * from (select SUBSTRING(mobile,1,3) as p from users) t where t.p='181'
以上方法可以正常查询到结果。
问:
想通过一些办法,把函数计算好的结果直接放到where条件里面,如下:select @t:=substring(mobile,1,3) from users where @t='181';
这种查询为什么查询不到结果?如果需要调整改怎么写?
查询没有问题,但是你查询到的结果是
@t:=substring(mobile,1,3)
类型,只有三个字符的字段你现在要得到的是手机号181字段对应的一条数据,不是一个残缺的字段
select
后面紧跟的是你的查询对象建议你用like模糊查询
select * from user wheres where mobile like '181%'