MySQL LIKE限制 [英] MySQL LIKE limiting

查看:168
本文介绍了MySQL LIKE限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的表中有一行,其值的格式如下-1,2,3,4,10,11,21,34等.

Basicly I have a row in my table, that has the following format for it's value - 1,2,3,4,10,11,21,34 etc..

我正在使用LIKE语句检索值,但是由于我将其与%<value>%一起使用,因此当搜索1时,它将返回11、21、1,依此类推.

I'm retrieving values with LIKE statement, but since I'm using it with %<value>%, when searching for 1 it returns 11, 21, 1 and so on.

如何限制它以返回基于一个的值?

How can I limit it, to return values based on one?

推荐答案

代替col LIKE '%<value>%'

col LIKE '%,<value>,%' OR col LIKE '<value>,%' OR col LIKE '%,<value>' OR col LIKE '<value>'

或更好

col REGEXP '(.+,|^)<val>(,.+|$)'

但是最好的解决方案是更改您的数据结构!要么使用SET类型,要么使用多个字段,要么使用一个联接表.

But the best solution is to change your data structure! Either use the SET type, or several fields, or a join table.

这篇关于MySQL LIKE限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆