select substring(custmer_name, 1, instr(custmer_name, ' ')) as first_name from sales.customers;
這個解決方案給了我答案,但它不適用於姓氏
P粉8845486192024-04-07 16:34:04
使用SUBSTRING_INDEX()需要3個參數:
您可以在此找到更多說明文章
查詢
SELECT SUBSTRING_INDEX(customer_name,' ', 1) as first_name, SUBSTRING_INDEX(customer_name,' ', -1) as last_name FROM customer;
P粉2760641782024-04-07 14:24:41
請測試一下:我使用locate函數來定義「 」的位置。
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;#