MySQL的搜索与逗号分隔的字符串 [英] Mysql search with comma delimited string

查看:90
本文介绍了MySQL的搜索与逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经1 MySQL表所谓的颜色与行 ID 命名

 1  - 黄色
2 - 黑
3 - 红
4 - 绿色
5 - 白
6 - 蓝

我怎样才能获得ID的数组,如果我有,例如,搜索字符串

 [颜色] =>串(14)蓝,红,白


解决方案

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

 从标签那里FIND_IN_SET(名字,$颜色)GT选择ID; 0

注:按以下丹的评论,此查询不使用索引,将是一个大的表慢。在查询是更好的:

 从选项卡中选择id其中名称(蓝,红,白)

I have 1 mysql table called colors with rows id and name

1 - yellow
2 - black
3 - red
4 - green
5 - white
6 - blue

How can I get array of IDs if I have, for example, search string

["colors"]=> string(14) "blue,red,white"

解决方案

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

select id from tab where find_in_set(name, '$colors') > 0

NB: as per Dan's comment below, this query doesn't use indexes and will be slow on a large table. A query with IN is better:

select id from tab where name IN ('blue', 'red', 'white')

这篇关于MySQL的搜索与逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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