搜索

首页  >  问答  >  正文

如何从 customer_name 列中获取名字

select substring(custmer_name, 1, instr(custmer_name, ' ')) as first_name from sales.customers;

这个解决方案给了我答案,但它不适用于姓氏

P粉908643611P粉908643611229 天前600

全部回复(2)我来回复

  • P粉884548619

    P粉8845486192024-04-07 16:34:04

    使用SUBSTRING_INDEX()需要3个参数:

    1. 列名称
    2. Delimiter
    3. 出现次数

    您可以在此找到更多说明文章

    查询

    SELECT
      SUBSTRING_INDEX(customer_name,' ', 1) as first_name, 
      SUBSTRING_INDEX(customer_name,' ', -1) as last_name FROM customer;

    回复
    0
  • P粉276064178

    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;

    结果集:

    回复
    0
  • 取消回复