Home >Backend Development >PHP Tutorial >MySQL查询的问题

MySQL查询的问题

WBOY
WBOYOriginal
2016-06-20 12:45:24948browse

如图,字段tid保存的是文章标签id,用逗号分隔的字符串,我现在想通过标签,来查出来所有的文章,比如“2”,来查出所有tid字段里有2的文章,现在我用SELECT * FROM `t_ms_article` WHERE `tid` IN (2)语句只能查出来以2开头的文章,而不能查出来‘1,2,3’这样的
希望能帮我一下,我是菜鸟


回复讨论(解决方案)

SELECT * FROM `t_ms_article` WHERE `tid` LIKE ‘%2%’;

SELECT * FROM `t_ms_article` WHERE `tid` LIKE ‘%2%’;


不行的,这样的话会把12、20这样的标签也给查出来的

SELECT * FROM `t_ms_article` WHERE find_in_set('2', `tid`)

SELECT * FROM `t_ms_article` WHERE CONCAT(',',`tid`,',') LIKE ‘%,2,%’;

SELECT * FROM `t_ms_article` WHERE find_in_set('2', `tid`)


多谢,一下就解决了,大神请收下我的膝盖!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn