select substring(custmer_name, 1, instr(custmer_name, ' ')) as first_name from sales.customers;
這個(gè)解決方案給了我答案,但它不適用於姓氏
使用SUBSTRING_INDEX()需要3個(gè)參數(shù):
您可以在此找到更多說明文章
查詢
SELECT SUBSTRING_INDEX(customer_name,' ', 1) as first_name, SUBSTRING_INDEX(customer_name,' ', -1) as last_name FROM customer;
請(qǐng)測(cè)試一下:我使用locate函數(shù)來定義「 」的位置。
SELECT LEFT(customer_name, LOCATE(' ',customer_name)-1) as first_name, RIGHT(customer_name, LENGTH(customer_name)-LOCATE(' ',customer_name)) as last_name FROM customer;#