du4822 2012-04-16 04:02
浏览 31
已采纳

mysql在文本字段中搜索catid

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

^^ 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.

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

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

  • 写回答

1条回答 默认 最新

  • dougu8742 2012-04-16 04:08
    关注

    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
    

    Which of course can be simplified into:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路