Mysql compare comma-separated field with single string

陌路散爱 提交于 2019-12-13 06:49:53

问题


So a field called schools in the database might have a value of:

'13,121,112,1212'

I'm using that to show the potential for a mistake.

Suppose I'm looking for a value of 12 in that field. The commas denote a "whole number" and I don't want to match 112 or 1212

Is there a more elegant match than this?

@compare = 12;
WHERE CONCAT(schools,',') LIKE CONCAT('%',compare,',%)

I was recently impressed by the GROUP_CONCAT function but this is kind of in reverse of that. Thanks!


回答1:


For this simple case you can use FIND_IN_SET();

WHERE FIND_IN_SET('13', schools);

Note though that there is no good indexing for columns with comma separated text, so the queries will be much slower than a normalized database.



来源:https://stackoverflow.com/questions/35902898/mysql-compare-comma-separated-field-with-single-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!