以下语句产生的所有内容都超过了vendor_name中的第一个单词。我需要它在使用子字符串时只产生vendor_name中的第二个单词。如果为NULL,我还需要它返回一个空白字段。这可能吗?
select substring_index(vendor_name, ' ',-2) as 'Second Word'
from vendors;
编辑:我认为它需要成为这些方面的东西,但我不能让它起作用: substring_index(substring_index(vendor_name,'',-2)vendor_name,'',1)as '第二个字'
答案 0 :(得分:1)
如果你只想要第二个单词,我会得到前两个单词,然后从那个结果中选出最后一个单词。这可以处理你可能有四个或更多单词的情况。
mysql> select substring_index('one two three four', ' ', 2) as s;
+---------+
| s |
+---------+
| one two |
+---------+
mysql> select substring_index(substring_index('one two three four', ' ', 2), ' ', -1) as s;
+-----+
| s |
+-----+
| two |
+-----+
答案 1 :(得分:0)
试试这个:
select
case
when instr(vendor_name,' ') = 0 then ' ' //This will show blank for null.
else substring_index(substring_index(vendor_name,' ',2),' ',-1)
end as second_name
from t;
它也会显示空白。
希望它有所帮助!