php查询mysql逗号,在PHP MySQL中从逗号分隔的字符串中查找确切值

博客讨论了在MySQL中使用正则表达式从以逗号分隔的类别ID字段中筛选特定产品的问题。当前的正则表达式导致了不期望的匹配,返回了包含目标ID的多个类别。解决方案是使用更精确的正则表达式,确保只匹配被逗号包围或位于字符串开头/结尾的目标ID。
摘要由CSDN通过智能技术生成

I have a product table which contains a field called 'categories' to save product related category ids as comma separated values. I am using regexp to search products from a category.

Assume there is record containing 4,24,1,31

my expression is,

..WHERE categories REGEXP ',?4,?'

but this returns both product of category 4 and 24

I just need to display only category 4.

Am I missing something?

解决方案

Use

WHERE categories REGEXP "(^|,)4(,|$)"

This matches 4 if surrounded by commas or at the start/end of the string.

In your present version, both commas are entirely optional, so the 4 in 24 matches.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值