MySQL 查询使用 'in' 运算符 : why different results w/quotes?

标签 mysql sql coercion

我在执行查询的两种不同方式中发现了一个奇怪的 MySQL 问题。当你把所有东西都归结起来时,这种方式会返回更多结果:

SELECT DISTINCT <stuff> FROM <tables> 
WHERE promo_detail_store_id in (8214, 8217, 4952, 8194, ...)

对 WHERE 子句的这种更改产生了这些结果的一个子集:

WHERE promo_detail_store_id in ('8214, 8217, 4952, 8194, ...')

(promo_detail_store_id 在 MyISAM 表中定义为 BIGINT。)

最初 store_ids 列表要长得多,我开始把它剪得越来越短,我想也许字符串的长度有一些奇怪的限制。但不,它也适用于非常小的字符串/列表。很明显,幕后发生了一些事情,涉及类型强制以及“in”运算符的工作方式。谁能赐教一下?

最佳答案

WHERE promo_detail_store_id in (8214, 8217, 4952, 8194, ...)

表示

WHERE promo_detail_store_id = 8214 
OR  promo_detail_store_id = 8217
OR promo_detail_store_id = 4952 
OR promo_detail_store_id = 8194
OR ... 

WHERE promo_detail_store_id in ('8214, 8217, 4952, 8194, ...')

表示

 WHERE promo_detail_store_id = '8214, 8217, 4952, 8194, ...'

'8214, 8217, 4952, 8194, ...' 将转换为数字为 8214,因此它将是

WHERE promo_detail_store_id = 8214

关于MySQL 查询使用 'in' 运算符 : why different results w/quotes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545331/

相关文章:

php - 在 mysql where in 子句中对数组使用内爆

php认证脚本

mysql - 查找销售人员拥有的潜在客户数量

sql - 将子查询直接传递给 SQL Server 中 USP 中的表类型参数

sql - "Invalid column name",按子查询别名排序

javascript - 随机数组生成的意外行为

mysql - 根据列值添加列数据?

sql - ORDER BY 取决于两个日期列

r - 为列表中的每个数据框创建一个包含列总和的新行

perl - 使用 Type::Tiny 将类型参数化为另一种类型