我有一個列表數組中的SQL select IN子句?
$list = array('a','b','c','d','e','f');
$filter_list = join(", " $list);
select * from test where id_col=12 and try_col in ($filter_list);
誰都看得出我怎樣才能做到這一點?
我有一個列表數組中的SQL select IN子句?
$list = array('a','b','c','d','e','f');
$filter_list = join(", " $list);
select * from test where id_col=12 and try_col in ($filter_list);
誰都看得出我怎樣才能做到這一點?
$list = array('a','b','c','d','e','f');
$filter_list = "'" . join("', '", $list) . "'";
$query = "select * from test where id=12 and try_col in ($filter_list)";
// select * from test where id=12 and try_col in ('a', 'b', 'c', 'd', 'e', 'f')
請注意,如果您的數組值包含'
,則將失敗。這是一種解決方法:
$list = array("a'a",'a\a','b','c','d','e','f');
$temp = array_map("addslashes", $list);
$query = "SELECT * FROM test WHERE id=12 AND try_col in ('" . implode("','", $temp) . "')";
// SELECT * FROM test WHERE id=12 AND try_col in ('a\'a','a\\a','b','c','d','e','f')
「試圖在$ list」部分是什麼意思? – Gabe
你怎麼能做什麼? – nos
是'嘗試'列名? –