Home  >  Q&A  >  body text

mysql - PHP数据库查询疑问

有一个文章表,文章表中有个关联标签的字段tag
tag字段的内容基本是这样的:1,2,3,333,230(关联的标签ID,用逗号隔开)
现在出现问题了,我现在想找到标签ID为3的文章,用LIKE %3%好像会把333,230标签的内容都查出来
在不改变表结构的情况下,有什么查询方法吗?

过去多啦不再A梦过去多啦不再A梦2712 days ago423

reply all(5)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:09:48

    SELECT * FROM test WHERE find_in_set('3',tag)

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:09:48

    tag LIKE '%,3' or tag LIKE '3,%' or tag LIKE '%,3,%'

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 13:09:48

    LIKE %,3,% just add a comma

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:09:48

    find_in_set, use this to meet your requirements, don’t use like....

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:09:48

    I recommend using another table to maintain the classification relationship. find_in_set is a full table scan without using indexes. There will be problems with the database by then

    reply
    0
  • Cancelreply