gpt4 book ai didi

php - SQL从数组中选择IN子句?

转载 作者:行者123 更新时间:2023-11-29 14:24:16 24 4
gpt4 key购买 nike

我有一个 list

$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')

关于php - SQL从数组中选择IN子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7104051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com