MySQL WHERE问题,以逗号分隔的列表 [英] Mysql WHERE problem with comma-separated list

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

问题描述

我需要这个问题的帮助.

I need help for this problem.

在MYSQL表中,我有一个字段:

In MYSQL Table i have a field :

Field  : artist_list  
Values : 1,5,3,401

我需要找到艺术家uid 401的所有记录

I need to find all records for artist uid 401

我这样做

SELECT uid FROM tbl WHERE artist_list IN ('401');

我有所有记录,其中artist_list字段的值仅是'401',但是如果我有11,401,则此查询不匹配.

I have all record where artist_list fields values are '401' only, but if i have 11,401 this query do not match.

有什么主意吗?

(我无法使用LIKE方法,因为如果艺术家uid为3(匹配30、33、3333)...

(I cant user LIKE method because if artist uid is 3 (match for 30, 33, 3333)...

推荐答案

短期解决方案

使用 FIND_IN_SET函数:

SELECT uid 
  FROM tbl 
 WHERE FIND_IN_SET('401', artist_list) > 0

长期解决方案

标准化您的数据-这似乎是已经涉及两个表的多对多关系.逗号分隔的列表需要变成一个自己的表:

Long Term Solution

Normalize your data - this appears to be a many-to-many relationship already involving two tables. The comma separated list needs to be turned into a table of it's own:

  • artist_id(主键,ARTIST的外键)
  • uid(主键,TBL的外键)

这篇关于MySQL WHERE问题,以逗号分隔的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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