MySQL ENUM column won't match quoted values after import to new machine

不问归期 提交于 2019-12-23 22:21:04

问题


Recently I've imported a new database to develop on my local machine, however it doesn't work: the ENUM column only works when the variable is sent without quotation marks. Here's an example:

mysql.local>select count(*) from psh_products where active = 1;
+----------+
| count(*) |
+----------+
|    72782 |
+----------+
1 row in set (0.04 sec)

mysql.local>select count(*) from psh_products where active = '1';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

In case you're wondering about the table structure:

CREATE TABLE `psh_products` (
  `productID` int(12) unsigned NOT NULL AUTO_INCREMENT,
  `catID` int(2) unsigned NOT NULL,
  `main_sku` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `sku` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `shortsku` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `upc` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  `quantity` int(2) unsigned NOT NULL,
  `buy_now` decimal(11,2) NOT NULL,
  `seller_cost` decimal(11,2) NOT NULL,
  `cdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `codedir` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `code` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `basic_colorID` int(2) unsigned NOT NULL,
  `manu_colorID` int(2) unsigned NOT NULL,
  `brandID` int(2) unsigned NOT NULL,
  `matID` int(2) unsigned NOT NULL,
  `sizeID` int(2) unsigned NOT NULL,
  `size_sID` int(2) unsigned NOT NULL,
  `styleID` int(2) unsigned NOT NULL,
  `featID` int(2) unsigned NOT NULL,
  `occID` int(2) unsigned NOT NULL,
  `widthID` int(2) unsigned NOT NULL,
  `width_sID` int(2) unsigned NOT NULL,
  `genderID` int(2) unsigned NOT NULL,
  `gender_sID` int(2) unsigned NOT NULL,
  `hits` int(2) unsigned NOT NULL,
  `active` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `tags` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tmp_img` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
  `imgact` enum('again','flip','resize','moderated','badimg','badimgm','badimga') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'moderated',
  `status` enum('new','moderated') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'moderated',
  `quanflag` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `deleted` enum('0','1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`productID`),
  KEY `idx_cid` (`catID`),
  KEY `idx_bid` (`brandID`),
  KEY `idx_act` (`active`),
  KEY `idx_act_hits` (`active`,`hits`),
  KEY `idx_wid` (`widthID`),
  KEY `idx_sid` (`sizeID`),
  KEY `idx_styleid` (`styleID`),
  KEY `idx_sku` (`sku`),
  KEY `idx_msku` (`main_sku`),
  KEY `idx_matid` (`matID`),
  KEY `idx_cid_mstyleid` (`catID`,`featID`),
  KEY `idx_shortsku` (`shortsku`),
  KEY `idx_quant` (`quantity`),
  KEY `idx_quanflag` (`quanflag`),
  KEY `idx_hits` (`hits`),
  KEY `idx_act_qua_cat` (`active`,`quantity`,`catID`),
  KEY `idx_act_qua_cat_sho` (`active`,`quantity`,`catID`,`shortsku`),
  KEY `idx_bcolor_id` (`basic_colorID`),
  KEY `idx_mcolor_id` (`manu_colorID`),
  KEY `idx_cdate` (`cdate`),
  KEY `idx_deleted` (`deleted`),
  KEY `occID` (`occID`),
  KEY `idx_fid` (`featID`),
  KEY `width_sID` (`width_sID`,`genderID`,`gender_sID`)
) ENGINE=InnoDB AUTO_INCREMENT=72790 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Any ideas are welcome!


回答1:


Note that when you use a numerical value in a query against an enum data type as in your first query, the numerical value is treated as an index, not as one of the enumerated values.

So, querying

select count(*) from psh_products where active = 1;

is really equivalent to

select count(*) from psh_products where active = '0';

since '0' is the first item (index 1) in the enumeration.

Because of this confusion, the documentation explicilty states "we strongly recommend that you do not use numbers as enumeration values."




回答2:


That was my mistake, I did

UPDATE psh_products SET ACTIVE = 1 WHERE ......

So the update overwrote the '1''s that were there with 1's.



来源:https://stackoverflow.com/questions/7574760/mysql-enum-column-wont-match-quoted-values-after-import-to-new-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!