MySQL query using 'in' operator: why different results w/ quotes?

让人想犯罪 __ 提交于 2019-12-10 20:20:42

问题


I've tracked down a weird MySQL problem to the two different ways I was performing a query. When you boil everything down, this way returns more results:

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

This change to the WHERE clause produces a subset of those results:

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

(promo_detail_store_id is defined as a BIGINT in a MyISAM table.)

Originally that list of store_ids was much longer, and I started cutting it shorter and shorter thinking maybe there was some weird limits on the length of a string. But no, it holds for quite small strings/lists too. Clearly something is going on behind the scenes involving type coercion and maybe how the 'in' operator works. Can someone enlighten me?


回答1:


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

means

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, ...')

means

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

'8214, 8217, 4952, 8194, ...' will cast to number to be 8214, so it will be

WHERE promo_detail_store_id = 8214



回答2:


The second condition is effectively equivalent to WHERE promo_detail_store_id IN (8214), because you're comparing a BIGINT to a TEXT and the text is cast to an integer value, which disregards everything from the first non-numeric character to the end of the string.



来源:https://stackoverflow.com/questions/12545331/mysql-query-using-in-operator-why-different-results-w-quotes

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