varchar 和 int 列之间的比较问题

Comparison issues between varchar and int columns

我的 varchar 专栏和 int 专栏之间存在一些奇怪的比较问题。

我的架构

CREATE TABLE `organization_m` (
    `organization_recid` VARCHAR(36) CHAR SET utf8 NOT NULL COMMENT 'uuid - unique identifier'
    ,`organization_id` INT (16) NOT NULL AUTO_INCREMENT
    ,`organization_name` VARCHAR(100) CHAR SET utf8 NOT NULL
    ,`organization_sequence` INT (11) DEFAULT NULL
    ,`license_recid` VARCHAR(36) CHAR SET utf8 NOT NULL
    ,`country_recid` VARCHAR(36) CHAR SET utf8 NOT NULL
    ,`contact_number` VARCHAR(20) CHAR SET utf8 DEFAULT NULL
    ,`address` VARCHAR(200) CHAR SET utf8 DEFAULT NULL
    ,`organization_url` VARCHAR(100) CHAR SET utf8 NOT NULL
    ,`notes` VARCHAR(500) CHAR SET utf8 DEFAULT NULL
    ,`is_active` TINYINT (1) DEFAULT NULL
    ,`created_on` DATETIME DEFAULT NULL
    ,`modified_on` DATETIME DEFAULT NULL
    ,`created_by` VARCHAR(50) CHAR SET utf8 DEFAULT NULL
    ,PRIMARY KEY (`organization_recid`)
    ,UNIQUE KEY `organization_id`(`organization_id`)
    ,UNIQUE KEY `organization_sequence`(`organization_sequence`)
    ) ENGINE = InnoDB AUTO_INCREMENT = 8622 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;

--
-- Dumping data for table `organization_m`
--
LOCK TABLES `organization_m` WRITE;
    /*!40000 ALTER TABLE `organization_m` DISABLE KEYS */;

INSERT INTO `organization_m`
VALUES (
    '017811ce-becf-4395-a780-9bde07c4a692'
    ,1268
    ,'123'
    ,NULL
    ,'cd224237-668b-11e4-945d-000c29609978'
    ,'cd2276db-668b-11e4-945d-000c29609978'
    ,NULL
    ,'somewhere'
    ,'/api/v1/somewhere'
    ,NULL
    ,1
    ,'2015-02-23 13:43:22'
    ,'2015-02-23 13:43:22'
    ,NULL
    )
    ,(
    '9f14dc52-23c7-4567-8ec9-90aa45c87799'
    ,1275
    ,'cards'
    ,NULL
    ,'cd224237-668b-11e4-945d-000c29609978'
    ,'cd227f0c-668b-11e4-945d-000c29609978'
    ,NULL
    ,'somewhere to nowhere'
    ,'/api/v1/cards'
    ,NULL
    ,1
    ,'2015-02-27 16:17:25'
    ,'2015-02-27 16:17:25'
    ,NULL
    )
    ,(
    'b469e572-86a4-4019-96c5-a6df964e520b'
    ,9
    ,'123'
    ,1143
    ,'cd224237-668b-11e4-945d-000c29609978'
    ,'cd227f0c-668b-11e4-945d-000c29609978'
    ,NULL
    ,'New York'
    ,'OSMO1143'
    ,NULL
    ,1
    ,'2015-01-20 13:45:40'
    ,'2015-01-20 13:45:40'
    ,NULL
    );
    /*!40000 ALTER TABLE `organization_m` ENABLE KEYS */;

UNLOCK TABLES;

我的Select查询

SELECT * FROM organization_m WHERE organization_id = '9f14dc52-23c7-4567-8ec9-90aa45c87799' OR 
organization_recid = '9f14dc52-23c7-4567-8ec9-90aa45c87799';

我的结果集


问题

我只期望一个结果,即 rec_id 的组织 organization_recid = '9f14dc52-23c7-4567-8ec9-90aa45c87799';。我不确定这是否是预期的行为,但有没有办法解决这个问题?


其他数据库信息

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1710
Server version: 10.0.16-MariaDB-1~wheezy mariadb.org binary distribution

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

数据库会自动将您作为 organization_id 传递的字符串转换为整数(因为这是它的类型)。

9f14dc52-23c7-4567-8ec9-90aa45c87799 转换为等于 9 的整数。(它在第一个字符处停止,因为第二个字符不是整数)。您可以通过执行以下查询来查看转换:

select CONVERT('9f14dc52-23c7-4567-8ec9-90aa45c87799', SIGNED);

这就是为什么您使用 organization_id = 9 获得第二行的原因。

基本上,您的查询被解释为:

SELECT * FROM organization_m WHERE organization_id = '9' 
OR  organization_recid = '9f14dc52-23c7-4567-8ec9-90aa45c87799'

我不知道该代码的上下文是什么,但是在查询中使用时,您应该始终过滤/清理值。

控制。如果您需要一个整数,请确保它是一个。否则,你最终会遇到奇怪的行为/错误。