Mysql string comma separated as%, which caused the result?%

I have a very difficult problem that my colleague and I have been discussing for a long time today, and we simply cannot answer this question by wondering if anyone has a bright idea here that we missed.

$tags=mysql_real_escape_string($_GET["tags"]);
$tags="SELECT * FROM i WHERE tip LIKE '%".$tags;
$tags=str_replace(",","%' OR tip LIKE '%",$tags);
$tags=$tags."%'";
$qtags = mysql_query($tags) or die(mysql_error());

example: $ tags = 'word, phrase, term, foo, bar, anything'

Tags

pulled from a paragraph written by the user, and then compared with paragraphs stored in "i" (sql database).

the problem we are facing is how to determine which / which "tag" (from the tag line) caused the result.

Example

: the resulting paragraph was like the tag “foo” from the string $ tags, which was “word, phrase, term, foo, bar, anything”, how can we identify foo as the reason?

+3
source share
1 answer

You can try something like this:

SELECT
    CASE WHEN tip LIKE '%foo%' then 'foo'
         WHEN tip LIKE '%bar%' then 'bar'
         WHEN tip LIKE '%anything%' then 'anything'
    END as MatchedTag,
    i.*
FROM i
WHERE tip LIKE '%foo%'
   OR tip LIKE '%bar%'
   OR tip LIKE '%anything%'

It will need to be generated from your variable $tags, as your current request.

+2
source

All Articles