suchen

Heim  >  Fragen und Antworten  >  Hauptteil

So erhalten Sie den Vornamen aus der Spalte „customer_name“.

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

Diese Lösung hat mir die Antwort gegeben, funktioniert aber nicht für den Nachnamen

P粉908643611P粉908643611259 Tage vor655

Antworte allen(2)Ich werde antworten

  • 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;

    Antwort
    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;

    结果集:

    Antwort
    0
  • StornierenAntwort