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 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备