在 MySQL 条件中使用子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5348153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:10:00 来源:igfitidea点击:
Using Substring in MySQL criteria
提问by Ian
I'm trying to fetch all instances where the 1st letter of a person's first name is equal to P.
我试图获取一个人的名字的第一个字母等于P 的所有实例。
This is what I came up with, which doesn't return anything:
这是我想出的,它不返回任何内容:
$sql="SELECT * FROM people WHERE SUBSTRING(FirstName,0,1) = 'P'";
Suggestions?
建议?
回答by Jim Garrison
The reason your expression doesn't work is that substring() positions are 1-based
您的表达式不起作用的原因是 substring() 位置是基于 1
Try either of these:
尝试以下任一方法:
where FirstName like 'P%'
or
或者
where substring(FirstName,1,1) = 'P'