search

Home  >  Q&A  >  body text

mysql - 如果一张表里有这么两行数据,数据中某一字段值为 10,100跟 100,99 时,如何找到值里有10的那一行?

如果只用sql语句是可以行的吗?

巴扎黑巴扎黑2788 days ago878

reply all(5)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 14:41:22

    select * from table where substring_index(field, ',', 1) = '10'
    截取第一个逗号前面的部分

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:41:22

    I don’t quite understand your question. According to my understanding, it is not simply select * from test where id = 10.

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:41:22

    Use fuzzy matching: select * from test where column like "%10%"
    PS: I feel that such a requirement is a bit strange, maybe the database design is unreasonable.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:41:22

    From the perspective of the question, the solution can be
    Custom function, used to segment fields, and then judge,
    1. Custom function, to be added
    2. Query statement
    select * from table where 1=hasTen(column)

    A rough compromise
    select * from table where column like '10,%'
    ||
    select * from table where column like '%,10,%'
    ||
    select * from table where column like '%,10'

    It may be easier to solve if you can ensure that the fields must be in the form of a and b

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:41:22

    For reference: select * from test where concat(',', column, ',') like "%,10,%"

    reply
    0
  • Cancelreply