【MySQL】字符串截取
字符串’aaa,bbb’
截取逗号前的字符串和截取逗号后的字符串
1:SUBSTRING_INDEX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mysql> SELECT SUBSTRING_INDEX('aaa,bbb',',',1); +----------------------------------+ | SUBSTRING_INDEX('aaa,bbb',',',1) | +----------------------------------+ | aaa | +----------------------------------+ 1 row in set mysql> SELECT SUBSTRING_INDEX('aaa,bbb',',',-1); +-----------------------------------+ | SUBSTRING_INDEX('aaa,bbb',',',-1) | +-----------------------------------+ | bbb | +-----------------------------------+ 1 row in set mysql> |
2:REVERSE+SUBSTRING_INDEX
reverse函数虽然很笨,但是这个思路可以记录一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mysql> SELECT REVERSE('aaa,bbb'); +--------------------+ | REVERSE('aaa,bbb') | +--------------------+ | bbb,aaa | +--------------------+ 1 row in set mysql> SELECT REVERSE(SUBSTRING_INDEX(REVERSE('aaa,bbb'),',',1)); +----------------------------------------------------+ | REVERSE(SUBSTRING_INDEX(REVERSE('aaa,bbb'),',',1)) | +----------------------------------------------------+ | bbb | +----------------------------------------------------+ 1 row in set mysql> |