dongsibao8977 2014-03-10 14:15
浏览 30
已采纳

就像在MySQL和PHP中以逗号分隔的列表一样

I have a user_apps table.

enter image description here

One of the column is appid which is a search able. As you can I see the picture of the column I have made a mistake as I am storing a coma seprated list.

In the record 3,4 and 6 there are ids as "2," "2" and "12,"

if I do a like '%2%' i get all three records - which is wrong.

if i do a like '%2,%' i get 2, and 12, and miss 2 - again it is wrong.

is there anyway i can sort it out at code level

  • 写回答

2条回答 默认 最新

  • duandu5846 2014-03-10 14:17
    关注

    Short term solution: find_in_set()

    SELECT ... WHERE FIND_IN_SET('2', appid)
    

    Long term + Real solution: Fix your database design and normalize it. Each of those separate numbers should be in its own record in a child table.

    Hackish/even worse solution:

    SELECT ... WHERE (appid = 2) OR (appid LIKE '%,2') OR (appid LIKE '2,%') OR (appid LIKE '%,2,%')
    

    That covers all possible ways your value can show up in a CSV string: by itself, at the end of the string, at the start of the string, or somewhere in the middle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?