douhui9192 2013-09-18 21:33
浏览 35

爆炸并检查是否存在

this is MySQL table structure:

TABLE: products

|----------------------------------------------|
|   id    |             product_ids            |
|   1     |              51                    |
|   2     |              616,2,38              |
|   3     |              (NULL)                |
|   4     |              426,605,604           |
|----------------------------------------------|

What im looking is a way for the code to check if the id 605 exists in product_ids row, and if it does to replace for another ID:

any idea how can i accomplish this?

I was thinking of a sql QUERY...

  • 写回答

5条回答 默认 最新

  • duanchangnie7996 2013-09-18 21:42
    关注

    To find out if it contains '605':

    SELECT id, product_ids FROM products 
    where product_ids REGEXP '^.*[^\d]605(?!\d).*$'
    

    Comment: this is, by the way, a very good example why we shouldn't use multiple values in the same field!

    Now once you have it - you can do whatever you want, you can play with it:

    $new_product_ids = preg_replace('/^(.*[^\d])(605)(?!\d)(.*)$/','${1}new_id${3}',$row1['product_ids']);
    

    and update the row:

    update products set product_ids = '$new_product_ids' where id = $id
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大