homerun
homerun

Reputation: 20755

PHP MYSQL: search word query

i need to submit a query to search posts with a specific tag. the cell in the database is written this way : "funny tech good" so every word describe a tag. so i have writed this code in order to get a posts with the "funny" tag.

'SELECT * FROM `posts` WHERE `post_tag` = "funny" ORDER BY `post_id` DESC LIMIT 50'

unfortunately , it gives me no result. what is the problem in here?

Upvotes: 0

Views: 1735

Answers (4)

alexis
alexis

Reputation: 50190

The query WHERE post_tag LIKE "%fun%" will also match "funny", "defunct", etc. But you're in luck, because MySQL also supports perl-style regular expressions and you can say

WHERE post_tag RLIKE "\bfun\b"

(Be sure the sql engine sees the backslashes). This will only match "fun" as a complete word.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175


'SELECT * FROM `posts` WHERE `post_tag` LIKE "%funny%" ORDER BY `post_id` DESC LIMIT 50'

//OR to get tags starting with funny
'SELECT * FROM `posts` WHERE `post_tag` LIKE "funny%" ORDER BY `post_id` DESC LIMIT 50'

Ref: LIKE

Upvotes: 0

Ajeet  Sinha
Ajeet Sinha

Reputation: 2345

Try this.

SELECT * FROM `posts` WHERE `post_tag` LIKE  "%funny%" ORDER BY `post_id` DESC LIMIT 50'

Upvotes: 0

Sirko
Sirko

Reputation: 74046

Use the LIKE keyword or store your tags in a 1-tag-per-entry fashion.

 SELECT * FROM `posts` WHERE `post_tag` LIKE "%funny%" ORDER BY `post_id` DESC LIMIT 50

This will however find tags, that have other tags included, so a search for "java" will also find "javascript",

Upvotes: 1

Related Questions