Heim  >  Fragen und Antworten  >  Hauptteil

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

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

巴扎黑巴扎黑2743 Tage vor829

Antworte allen(5)Ich werde antworten

  • 巴扎黑

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

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

    Antwort
    0
  • 高洛峰

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

    不太明白你的问题,按照我的理解不就是简单的select * from test where id = 10。

    Antwort
    0
  • 迷茫

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

    使用模糊匹配:select * from test where column like "%10%"
    PS:感觉这样的需求有些奇怪,是否数据库设计不合理。

    Antwort
    0
  • 天蓬老师

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

    从题意上看,解决办法可以是
    自定义函数,切分字段用的,然后再判断,
    1.自定义函数,待补
    2.查询语句
    select * from table where 1=hasTen(column)

    粗暴妥协一点的做法
    select * from table where column like '10,%'
    ||
    select * from table where column like '%,10,%'
    ||
    select * from table where column like '%,10'

    如果能够保证字段一定是a,b的形式或许会好解决一点

    Antwort
    0
  • 天蓬老师

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

    参考一下:select * from test where concat(',', column, ',') like "%,10,%"

    Antwort
    0
  • StornierenAntwort