0

I want to search a variable with mysql query. But I want to get results that contains exact word. For example;

1 - car, cat, dog, bus, #train, #table
2 - duck, internet, dog, fish, table
3 - link, image, #html, code, php, #train
4 - tiger, lion, ship, plane, #tables
5 - crazy, music, #portable, new york
6 - animal, #table, dj, teacher

Page: website.com/e.php?tag=table

$tag = $_GET['tag']; //(my variable)

I want to get results that contains #table (JUST 1st and 6th)

So what kind of mysql query should i use?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Cuba Libre
  • 35
  • 4
  • That is the problem with delimited value lists in any SQL. You might want to start with http://dev.mysql.com/doc/refman/5.7/en/regexp.html. You _could_ start with `LIKE`, but then for you example it would be `X LIKE '%table%' AND X NOT LIKE '%tables%'`; which would give the wrong result if someone ever used `stable`, `tabled`, etc... – Uueerdo May 02 '16 at 16:57
  • Though.... you might be able to get away with `X LIKE 'table,%' OR X LIKE '%, table,%' OR X LIKE '%, table'` – Uueerdo May 02 '16 at 17:02
  • Is there any way to do this? When I search table, I don't want to see portable or tables on the results. Just wanna see "table". It shouldnt be so hard to do that :/ – Cuba Libre May 02 '16 at 17:07
  • I've tried this one but doesn't work; `$query = mysql_query("SELECT * FROM table WHERE name LIKE '#$tag,%' OR X LIKE '%,#$tag,%' OR X LIKE '%,#$tag");` – Cuba Libre May 02 '16 at 17:18
  • Judging from your sample data, you're missing the spaces. What is the `#` for? Also, replace all `X` with the field name. – Uueerdo May 02 '16 at 17:22
  • `#` is for `hashtag`. Please look at `1st` and `6th` sample. They have `#table` and `3rd` sample has just `table`. I don't want to see 3rd one on the results. Just 1st one and 6th one. So I have to use `#`. Btw sorry for all X. Its my fault I forgot'em all. :( – Cuba Libre May 02 '16 at 17:29
  • The like list should work then, though if `#` is in the `$tag` you shouldn't need it separately. (Was just thrown on the `#` because your `e.php?tag=table` omits it.) – Uueerdo May 02 '16 at 17:30
  • `$tag= $_GET['tag'];` `$query = mysql_query("SELECT * FROM photos WHERE description LIKE '$tag,%' OR description LIKE '%,$tag,%' OR description LIKE '%,$tag");` `while($row=mysql_fetch_assoc($query)){ .. }` Unfortunately it doesn't work either. – Cuba Libre May 02 '16 at 17:40
  • You're still omitting the spaces after the `,`'s – Uueerdo May 02 '16 at 18:19
  • Are you married to storing your tags that way? This becomes a piece of cake if you make a separate tags table and join it in your query, something like `SELECT * from stuff_table JOIN tags ON tags.stuff_id = stuff_table.id WHERE tags.tag_name = $my_properly_escaped_tag` – Jerry May 02 '16 at 22:02
  • Does this answer your question? [MySQL - Search exact word from string](https://stackoverflow.com/questions/42036956/mysql-search-exact-word-from-string) – Mark Rotteveel Feb 29 '20 at 09:16

0 Answers0