MySQL Select Like

I have a field in my table for tags on some content, separated by spaces, and at the moment I'm using:

SELECT * FROM content WHERE tags LIKE '%$selectedtag%'"

But if the selected tag is an elephant , it will select content with bigelephant and elephantblah tags , etc.

How can I just choose what I want?

+3
source share
7 answers
SELECT * FROM content WHERE tags RLIKE '[[:<:]]elephant[[:>:]]'

If your table MyISAM, you can use this:

SELECT * FROM content WHERE MATCH(tags) AGAINST ('+elephant' IN BOOLEAN MODE)

which can be significantly improved by creating an index on it FULLTEXT:

CREATE FULLTEXT INDEX ix_content_tags ON content (tags)

but will work even without an index.

To do this, you need to set up @@ft_min_wold_lenindex marks less than 4 characters long if you have one.

+5

MySQL.

SELECT * FROM content WHERE tags REGEXP '(^| )selectedtag($| )'

, , .

, , , ; : "-". , .

SELECT * FROM content WHERE tags LIKE '% selectedtag %'
+1

. " ", tags . , .

, , .

0

A: , contentid , :

select a.*
from content a,tags b
where a.id=b.contentid
group by a.id;

B: befor , ",bigelephant,elephant,", like "%,elephant,%"

0
WHERE tags LIKE '% '{$selectedtag}' %'
      OR tags LIKE ''{$selectedtag}' %'
      OR tags LIKE '% '{$selectedtag}''
0

"%" SQl. , , .

-1

, , , :

SELECT * FROM content WHERE tags LIKE '% elephant %';

, ( , )

Again, the best option is to set up a many-to-many relationship in your database, but I suspect you're looking for a quick and dirty one-time fix.

-1
source

Source: https://habr.com/ru/post/1731582/


All Articles