user1574268
user1574268

Reputation:

Search a table with comma delimited string?

I have a column in my table called tags. It has comma delimited text in it (box, hat, car).

I select a row from my table and I want to find other rows that have similar tags to the tags in the row selected.

I know it could be a better table design, but I can't change the design.

I know this will search the tags for a keyword, but I don't want to search via a keyword but by a list of tags.

WHERE (',' + Tags + ',') LIKE '%,keyword,%'

Does anyone know how I would do this?

Using MYSQL.

Upvotes: 2

Views: 976

Answers (1)

O. Jones
O. Jones

Reputation: 108676

You say you can't change the design. Can you add a FULLTEXT index and put this table in the MyISAM access method? If so you can use FULLTEXT searching. For your application you'll do best using BOOLEAN mode.

WHERE MATCH (tags)
      AGAINST ('box hat -car' IN BOOLEAN MODE);

This particular search looks for keywords box and hat, and excludes the keyword car.

Here's a description. This might work well for you. http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

You can run a FULLTEXT search in InnoDB. But I don't believe you can index it.

If you can't use a fulltext search, you're stuck with the wrong tool for your job. Matching comma-delimited strings with SQL is like driving nails by hitting them with the handle of a screwdriver. Both take a long time and are incredibly awkward.

Upvotes: 2

Related Questions