逗号分隔值&通配符在mysql [英] Comma separated value & wildcards in mysql

查看:162
本文介绍了逗号分隔值&通配符在mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有一个以逗号分隔的数据,例如

I have a value in my database with comma separated data eg.

11,223,343,123

我想获取数据,如果它匹配某个数字(在这个例子中是数字223)。

I want to get the data, if it match a certain number (in this example it's number 223).

WHERE wp_postmeta.meta_value IN
('223', '223,%', '%,223,%', '%,223')

我想我可以使用通配符,但没有运气。任何想法如何做到这一点?

I thought I could use wildcard for it, but with no luck. Any ideas of how to do this? Maybe it's better to do this using PHP?

推荐答案

使用逗号分隔的列表存储内容通常是一个坏主意,但如果你必须使用

Storing stuff in a comma separated list usually is a bad idea, but if you must, use the FIND_IN_SET(str,strlist) function.

WHERE FIND_IN_SET('223',wp_postmeta.meta_value)

如果您可以更改数据库并将其归一化,则会得到更快的结果。创建一个额外的表,将meta_values与您的表格中的primary_id相关联。

If you can change your database and normalise it, you would get faster results. Create an extra table that links meta_values to your primary_id in your table.

这篇关于逗号分隔值&通配符在mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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