Search code examples
mysqlvarchar

Select by varchar column with IN() part in condition and int value returns all rows


Can someone explain me why with that query:

SELECT * FROM `tags` WHERE (tag IN ('willa-lentza', 2016))

it return me all rows from tags table, but when I put 2016 into quotes it works good ?

tag column is varchar type.

SAMPLE ENVIRONMENT

CREATE TABLE  `tags` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `tag` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

INSERT INTO `tags` (`id`, `tag`) VALUES
  (1, '2016'),
  (2, 'plum'),
  (3, 'banana'),
  (4, 'apple'),
  (5, 'willa-lentza');

I also get the same error as Roland Bouman got:

Truncated incorrect DOUBLE value: 'willa-lentza'

Solution

  • You should never mix quoted and unquoted values in an IN list because the comparison rules for quoted values (such as strings) and unquoted values (such as numbers) differ. Mixing types may therefore lead to inconsistent results.

    http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in