I'm building a php script that creates a query string like,
$products = array(873, 874, 875);
$sql = 'select * from product_thumbnails where product in (' . implode($products, ', ') . ')';
which creates a query like
'select * from product_thumbnails where product in (873, 874, 875)'
The datatype for product is int. Are the params to the in() function treated as strings? If so, that would leave mysql to compare ints to strings and thus probably lead to a slow query as the app grows. Is there a way to typecast the params or to tell mysql to treat them as ints? Or is mysql smart enough to juggle this on its own?
Edit
Thanks much to Jerodev and jilesh for the answer- Mysql will treat these as ints. If you wanted a string instead, just use quotes inside the query string like
' in ("stringtype", "stringtype") '
In MySQL syntax... When expecting a number, you can provide a number 123
or a string "123"
.
When expecting a string, you can provide either, but when comparing a VARCHAR
to an unquoted number (123
), it goes to the extra effort of converting the VARCHAR
to numeric, which often gives you the wrong answer: "abc"
is treated as 0
, or at least something confusing: "123abc"
is treated as 123
.