Match tags in MYSQL

妖精的绣舞 提交于 2019-12-24 20:50:19

问题


I wanted to know how can you search in tags p.s. I have a table where there is a column for tags "bar,barmen,drink".

In search for example people search for barmen drink, how do I match this, I tried LIKE, INSTR() but no luck, the point is that tags column content doesn't have spaces between. It's like "bar,barmen,drink" not like "bar, barmen, drink"

Any help..?


回答1:


This is a problem inherent with this type of design. Short of changing how you store the data, you'd have to do a search like this:

SELECT * FROM my_table WHERE CONCAT(',', tags, ',') LIKE '%,bar,%';

EDIT: Actually, I just double-checked, and you can use regexp like so:

SELECT * FROM my_table WHERE tags REGEXP '[[:<:]]bar[[:>:]]';

Which should be quite a bit faster.




回答2:


Why not have a separate tags table, with

id int(11) auto_increment
post_id int(11)
tag text

Then, have one tags row for each tag.



来源:https://stackoverflow.com/questions/7158778/match-tags-in-mysql

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