如何将数组传递给IN()子句?

时间:2015-10-14 08:31:50

标签: php mysql arrays

我有一个这样的数组:

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches); // output is two number: 11, 12

现在我想知道,如何将此数组$matches传递给此查询?

SELECT column_name(s)
FROM table_name
WHERE column_name IN ($matches); // actually I want something like this: IN (11, 12)

有没有解决方案?

1 个答案:

答案 0 :(得分:5)

使用implode -

"SELECT column_name(s)
FROM table_name
WHERE column_name IN (" .implode(',', $matches[0]) . ")";