Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 858267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:31:18+00:00 2026-05-15T08:31:18+00:00

I have this Tags table CREATE TABLE IF NOT EXISTS `Tags` ( `id_tag` int(10)

  • 0

I have this Tags table

CREATE TABLE IF NOT EXISTS `Tags` (
   `id_tag` int(10) unsigned NOT NULL auto_increment,
   `tag` varchar(255) default NULL,
   PRIMARY KEY  (`id_tag`),
   UNIQUE KEY `tag` (`tag`),
   KEY `id_tag` (`id_tag`),
   KEY `tag_2` (`tag`),
   KEY `tag_3` (`tag`),
   KEY `tag_4` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2937 ;

INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
   (1816, '(class'),
   (2642, 'class\r\n\r\nâ?¬35'),
   (1906, 'class\r\nif'),
   (1398, 'class'),
   (2436, 'class)'),
   (1973, 'class:\n1.'),
   (2791, 'classes'),
   (1325, 'New'),
   (2185, 'pack'),
   (1905, 'packed'),
   (1389, 'WebClass');

I want to fetch all records where tag matches keywords class or pack or new, along with another field which indicates which of the 3 keywords actually matched with the tag field.

The following query does not give correct results
Query 1

select id_tag,
case tag 
   when tag LIKE "%class%" then "class" 
   when tag LIKE "%new%" then "new"
   when tag LIKE "%pack%" then "pack"
end as matching_tag 
from Tags 
where tag LIKE "%class%" OR tag LIKE "%new%" OR tag LIKE "%pack%"

I have to use the like inside the case. Otherwise complete matching works. The following query works:-

Query 2

select id_tag,
case tag 
   when "class" then "class" 
   when "new" then "new"
   when "pack" then "pack"
end as matching_tag 
from Tags 
where tag = "class" OR tag = "new" OR tag = "pack"

What is wrong with the query 1. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:31:18+00:00Added an answer on May 15, 2026 at 8:31 am

    Mysql supports two variants of case, the one you use in query 2 is less flexible but supports only equality on a single variable. The other version specifies no variable after case and then conditions need not be only equality:

    select id_tag,
    case  
       when tag LIKE "%class%" then "class" 
       when tag LIKE "%new%" then "new"
       when tag LIKE "%pack%" then "pack"
    end as matching_tag 
    from Tags 
    where tag LIKE "%class%" OR tag LIKE "%new%" OR tag LIKE "%pack%"
    

    See documentation for further details

    EDIT:
    Here’s a bit more explanation on why your query #1 returned what it returned:

    case tag
       when tag LIKE "%class%" then "class" 
       when tag LIKE "%new%" then "new"
       when tag LIKE "%pack%" then "pack"
    end as matching_tag
    

    expects to get a literal value for comparison between when ... then
    In the above case the expressions tag LIKE "%class%", tag LIKE "%new%" and tag LIKE "%pack%" are all evaluated before the actual case comparison.
    However (!), what happens is that they become either 0 or 1 and when compared to the value of tag it is the first value of 0 that will match any char (char will get cast to 0) – this is consistent with the results of your first query.

    Here’s a query that shows the logical values for the relevant expressions:

    select id_tag, tag LIKE "%class%", tag LIKE "%new%", tag = 0, case tag     when tag LIKE "%class%" then "class"     when tag LIKE "%new%" then "new"    when tag LIKE "%pack%" then "pack" end as matching_tag  from Tags  where tag LIKE "%class%" OR tag LIKE "%new%" OR tag LIKE "%pack%";
    

    That’s why you get unexpected results; the silent CAST is a standard pitfall here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables; CREATE TABLE IF NOT EXISTS `tags` ( `tag_id` int(11)
I have a MySQL table: CREATE TABLE documents ( id INT NOT NULL AUTO_INCREMENT,
I have 2 tables: posts tags Tags table is structured like this: post_id tag
First execute these tables and data dumps :- CREATE TABLE IF NOT EXISTS `Tags`
i have 2 tables CREATE TABLE if not exists tag_name( + tagid INTEGER PRIMARY
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have this script that only lets users enter a single tag but I
I have this query that should display all posts related to a specific tag
I have a javascript function which get datagrid's header into THEAD tags. This code
I have stored my tags in the SQL Server database, TABLE NAME: Tags COLUMNS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.