在MySQL条件中使用子字符串 [英] Using Substring in MySQL criteria
本文介绍了在MySQL条件中使用子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取一个人的名字的第一个字母等于 P 的所有实例.
I'm trying to fetch all instances where the 1st letter of a person's first name is equal to P.
这是我想出的,不会返回任何内容:
This is what I came up with, which doesn't return anything:
$sql="SELECT * FROM people WHERE SUBSTRING(FirstName,0,1) = 'P'";
建议?
推荐答案
表达式不起作用的原因是substring()位置是基于1的
The reason your expression doesn't work is that substring() positions are 1-based
尝试以下任一方法:
where FirstName like 'P%'
或
where substring(FirstName,1,1) = 'P'
这篇关于在MySQL条件中使用子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文