如何从类似sql的查询中获取精确值
我有一个 Mysql 数据库,其中包含一些类别 ID,它在表中存储逗号分隔的值。
通过使用 select * from style wherecategories like '%8,% ';
它返回所有以 8 结尾的值。例如,如果表行有两个值,例如 8 和 148,它将返回这两行。但我只想获取包含 8 的行。该怎么做
I have a Mysql database that contains some category ids on it which stores comma-separated values on a table.
By using select * from style where categories like '%8,%';
it returns all the values end with 8. For example, if the table rows have two values like 8 and 148 it returns both rows. But I want to get only the rows that contain 8. How to do it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在单个列中存储多个值是一种非规范化设计,几乎总是会给您带来问题。但是您需要在两边添加逗号并进行比较:
Storing multiple values in a single column is a denormalised design that will almost always cause you problems. However you need to add commas to both sides and compare:
和其他人一样:标准化你的数据。但如果你不能,mySQL 支持
find_in_set()
的set
数据类型,这似乎是。SQL
或我们可以使用布尔求值并消除 > 0
给我们:
注释
Like everyone else: normalize your data. But if you can't mySQL supports
find_in_set()
forset
datatypes which this appears to be.SQL
OR we can use a boolean evaluation and eliminate the > 0
Giving us:
Notes
您还可以使用 rlike,事实上它比 like 好得多,因为它有更多的选项。
还有更多选择
You can also use rlike and in fact it is much better than like as it has much more options.
And many more options