MySQL: select first element of a comma-separated list

前端 未结 2 637
春和景丽
春和景丽 2020-12-19 10:51

Unfortunately, I have a field column which contains values like

  • 4
  • 12,3
  • 8,5,6,7

I\'m going to write a SELECT statement, whose r

相关标签:
2条回答
  • 2020-12-19 11:18

    You can use MySQL function SUBSTRING_INDEX(str,delim,count)

    SELECT SUBSTRING_INDEX(value,',',1) As value FROM ...
    
    0 讨论(0)
  • 2020-12-19 11:27

    Use MySQL's SUBSTRING_INDEX function:

    SELECT SUBSTRING_INDEX(field, ',', 1)
    

    However, keeping lists in delimiter-separated strings is generally an inefficient use of a relational database management system like MySQL: it is often better to normalise your data structure by keeping such lists in a separate table of (id, value) pairs.

    0 讨论(0)
提交回复
热议问题