How can you return a string minus the first character in a string in MySQL?
如何在MySQL中返回字符串减去字符串中的第一个字符?
In other words, get 'ello' from 'hello'.
换句话说,从'你好'得到'ello'。
The only way I can think to do it is to use mid() with the second offset being larger than the string could possibly be:
我能想到的唯一方法是使用mid(),第二个偏移大于字符串可能是:
select mid('hello', 2, 99)
But I'm convinced there must be a more elegant way of doing it. Is there?
但我确信必须有一种更优雅的方式。在那儿?
3 个解决方案
#1
Use SUBSTR
(or SUBSTRING
):
使用SUBSTR(或SUBSTRING):
SELECT SUBSTR( 'hello', 2 );
--> 'ello'
See also: MySQL String Functions
另请参见:MySQL字符串函数
#2
That is actually incorrect. If you do that, you only get llo. In order to get everything after the first character....do the following:
这实际上是不正确的。如果你这样做,你只会得到llo。为了获得第一个字符后的所有内容....执行以下操作:
<?php
echo substr('hello', 1); // Output ello
echo '<br />';
echo substr('hello', 2); // Output llo
?>
I just wanted to correct that.
我只是想纠正这个问题。
Edit: Bah, ignore me. I thought you were talking about in PHP. Well anyway, you can do that in Mysql..or you can just get it in PHP as I posted above.
编辑:呸,不理我。我以为你是用PHP谈论的。好吧无论如何,你可以在Mysql中做到这一点。或者你可以在我上面发布的PHP中获取它。
#3
what about select substr('hello', 2)?
那么选择substr('你好',2)呢?
#1
Use SUBSTR
(or SUBSTRING
):
使用SUBSTR(或SUBSTRING):
SELECT SUBSTR( 'hello', 2 );
--> 'ello'
See also: MySQL String Functions
另请参见:MySQL字符串函数
#2
That is actually incorrect. If you do that, you only get llo. In order to get everything after the first character....do the following:
这实际上是不正确的。如果你这样做,你只会得到llo。为了获得第一个字符后的所有内容....执行以下操作:
<?php
echo substr('hello', 1); // Output ello
echo '<br />';
echo substr('hello', 2); // Output llo
?>
I just wanted to correct that.
我只是想纠正这个问题。
Edit: Bah, ignore me. I thought you were talking about in PHP. Well anyway, you can do that in Mysql..or you can just get it in PHP as I posted above.
编辑:呸,不理我。我以为你是用PHP谈论的。好吧无论如何,你可以在Mysql中做到这一点。或者你可以在我上面发布的PHP中获取它。
#3
what about select substr('hello', 2)?
那么选择substr('你好',2)呢?