MySQL Select Query - 仅获取值的前 10 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14959166/
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
MySQL Select Query - Get only first 10 characters of a value
提问by getvivekv
Ok, so here is the issue.
好的,所以这里是问题。
I have a table with some columns and 'subject' is one of the columns. I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
我有一个包含一些列的表格,“主题”是其中一列。无论“主题”字段包含一个包含 100 个字母的字符串,我都需要从“主题”字段中获取前 10 个字母。
For example,
例如,
Table - tbl
.
Columns - id
, subject
, value
.
表 - tbl
。列 - id
, subject
, value
.
SQL Query:
SQL查询:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
我得到的结果是,例如
Hello, this is my subject and how are you
你好,这是我的主题,你好吗
I only require the first 10 characters
我只需要前 10 个字符
Hello, thi
你好,这个
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
我可以理解我可以使用 php substr() 删除其余的字符,但在我的情况下这是不可能的。我需要让 MySQL 删除多余的字符。如何才能做到这一点?
回答by MuhammadHani
回答by Rajesh Paul
SELECT SUBSTRING(subject, 1, 10) FROM tbl
回答by Steve
Have a look at either Leftor Substringif you need to chop it up even more.
如果您需要进一步切碎它,请查看Left或Substring。
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
谷歌和 MySQL 文档是一个很好的起点 - 如果你在提问之前甚至没有尝试过帮助自己,你通常不会得到如此热情的回应。