MySQL query to extract first word from a field

后端 未结 4 502
夕颜
夕颜 2021-02-03 19:20

I would like to run a query that returns the first word only from a particular field, this field has multiple words separated by spaces, I assume I may need to carry out some re

相关标签:
4条回答
  • 2021-02-03 19:42
    select 
        substring(test_field, 1, instr(test_field, ' ')) 
    from 
        test_table
    
    0 讨论(0)
  • 2021-02-03 19:59
    SELECT
      SUBSTR(field_name, 1, LOCATE(' ', field_name)) AS first_word
    FROM
      table
    
    0 讨论(0)
  • 2021-02-03 20:00

    SUBSTRING_INDEX: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

    SELECT SUBSTRING_INDEX(`name`, ' ', 1);
    
    0 讨论(0)
  • 2021-02-03 20:05

    Here you go :)

    SELECT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`
    FROM `your_table`
    
    0 讨论(0)
提交回复
热议问题