本文介绍了mysql在文本字段中搜索catid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文本字段内进行mysql搜索(在php中)以获取catid号.我目前正在使用LIKE,但我需要更具体的信息.这是我的代码:

I'm trying to do a mysql search (in php) for a catid number within a text field. I'm currently using LIKE but I need it more specific. Here's my code:

SELECT * FROM articlepix
WHERE published = 1 AND catid LIKE '%86%'
ORDER BY RAND() LIMIT 1

^^我正在catid字段中查找数字86,但我希望它仅查找86,目前它也正在提取数字186的数据.

^^ I'm looking for the number 86 within the catid field, but I want it to look for only 86, currently it's pulling out the data with the number 186 too.

上面的示例抓取了catid为"91,107,36,139,146,168,186"的字段.

The above example grabs the field where the catid is "91,107,36,139,146,168,186".

是否可以仅搜索"86"而不是包含%86%的所有内容?如果说得通?

Is it possible to search for just '86' rather than everything that includes %86%? If that makes sense?

推荐答案

MySQL可以通过功能 FIND_IN_SET .试试看:

MySQL can help you workaround this huge database design errors with function FIND_IN_SET. Give this a try:

SELECT * FROM articlepix
WHERE published = 1 AND FIND_IN_SET('86', catid) > 0
ORDER BY RAND() LIMIT 1

当然可以简化为:

WHERE published = 1 AND FIND_IN_SET('86', catid)

因为该函数仅在没有匹配项时返回0,但可能很难理解它的作用:)

As the function only returns 0 when there is no match, but it might be harder to understand what it does :)

这篇关于mysql在文本字段中搜索catid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 08:47