dongyou1926 2011-09-16 04:20
浏览 39
已采纳

如何在逗号分隔的字符串中选择一个数字?

I have a column in my MySQL table. The column name is "attached_products", now, for example I have some records there and the attached_products have a value "1,16,164,1645", how do I select all records with "16"?

I tried the where clause, it went something like:

select * from websites where attached_products like '%16%'

but it also select the records with 164 and 21645.

Thanks for any help! :)

  • 写回答

4条回答 默认 最新

  • duanhongqiong9460 2011-09-16 04:26
    关注

    since you are using a bad design for your database you could use, like you said:

    SELECT * FROM `websites` WHERE (`attached_products` LIKE '%,16,%' OR `attached_products` LIKE '16,%' OR `attached_products` LIKE '%,16' OR `attached_products`='16');
    

    but you should consider redesigning your database for easier selects. You can add another table with only those attached products like:

    table 1
    id | etc ... | etc .... |
    
    attached_products
    id | id_product | product
    

    where id_product is from table 1 and product is the product id

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料