Home > Backend Development > PHP Tutorial > 查找字段的某位数相同的数据

查找字段的某位数相同的数据

WBOY
Release: 2016-06-06 20:09:36
Original
977 people have browsed it

想要查找uid倒数第二位的数值一样的数据
比如
1000000005
1000000006
1000000007
1000000008
1000000023 //不同的不要
1000000009

要实现这样的逻辑,怎么办

<code>$uid = I('get.uid', 0, 'intval');//假设用户ID:1000000005
$where['uid'] = substr($uid, -2,1);//倒数第二位:0
$res = M('code')->where($where)->select();//这样做实现不了</code>
Copy after login
Copy after login

回复内容:

想要查找uid倒数第二位的数值一样的数据
比如
1000000005
1000000006
1000000007
1000000008
1000000023 //不同的不要
1000000009

要实现这样的逻辑,怎么办

<code>$uid = I('get.uid', 0, 'intval');//假设用户ID:1000000005
$where['uid'] = substr($uid, -2,1);//倒数第二位:0
$res = M('code')->where($where)->select();//这样做实现不了</code>
Copy after login
Copy after login

<code>$where['uid'] = 'substr('+$uid+', -2,1)';
</code>
Copy after login

加上引号试一下,不加引号变成php中的函数了,mysql 也有substr函数的

你要生成的是类似这样的语句

<code>select * from `character` where SUBSTR(characterId, -2,  1) ='1'</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template