0

我很困惑,当我发现将值设置为 0 实际上选择了一些行。O_o 0 是数字,而不是字符串...这是我不知道的东西?mysql的某些特定功能还是我做错了什么?

这是查询:

SELECT * FROM contacts WHERE owner=0

结果是:

contact_id,owner,contact
10,d9659deb29f21dcf468783f1e7f52aa2b2ab6b48,7d1d00cd5cc06c27b3bcbefa8b4aeeb16bdec14e

这是测试转储:

-- --------------------------------------------------------
-- Host:                         127.0.0.1
-- Server version:               5.5.8 - Source distribution
-- Server OS:                    Linux
-- HeidiSQL version:             7.0.0.4053
-- Date/time:                    2012-08-02 11:05:48
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;

-- Dumping database structure for test
CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `test`;


-- Dumping structure for table test.contacts
CREATE TABLE IF NOT EXISTS `contacts` (
  `contact_id` int(10) NOT NULL AUTO_INCREMENT,
  `owner` varchar(40) DEFAULT '',
  `contact` varchar(40) DEFAULT '',
  PRIMARY KEY (`contact_id`),
  KEY `owner` (`owner`),
  KEY `contact` (`contact`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- Dumping data for table test.contacts: ~2 rows (approximately)
/*!40000 ALTER TABLE `contacts` DISABLE KEYS */;
REPLACE INTO `contacts` (`contact_id`, `owner`, `contact`) VALUES
    (9, '7d1d00cd5cc06c27b3bcbefa8b4aeeb16bdec14e', 'd9659deb29f21dcf468783f1e7f52aa2b2ab6b48'),
    (10, 'd9659deb29f21dcf468783f1e7f52aa2b2ab6b48', '7d1d00cd5cc06c27b3bcbefa8b4aeeb16bdec14e');
/*!40000 ALTER TABLE `contacts` ENABLE KEYS */;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
4

3 回答 3

1

我敢打赌:OWNER 是保留关键字。将您的选择更改为

SELECT * FROM contacts WHERE contacts.owner=0 
于 2012-08-02T08:18:22.127 回答
0

使用引号将数字与字符数据类型列进行比较:

SELECT * FROM contacts WHERE owner = '0';

CAST或使用以下函数进行类型转换:

SELECT * FROM contacts WHERE owner = CAST(0 AS CHAR);
于 2012-08-02T08:17:05.537 回答
0

所有者 '7d1d00cd5cc06c27b3bcbefa8b4aeeb16bdec14e' 以数字 '7...' 开头,MySQL 将此值转换为 7,我们有 7 = 0 - FALSE

所有者 'd9659deb29f21dcf468783f1e7f52aa2b2ab6b48' 以字符 'd...' 开头,MySQL 将此值转换为 0,因此 0 = 0 - TRUE

This is how MySQL converts strings to numbers.

于 2012-08-02T08:21:54.887 回答