$skuArray = array(00240=>123,00241=>456);
$getSkus = mysql_query("SELECT sku FROM data WHERE sku IN($skuArray)");
我上面的代码不起作用,如何让它选择所有sku的FROM数据WHERE sku = $ skuArray中的任何键名? (在这种情况下为00240和00241)
希望这是有道理的,谢谢。
答案 0 :(得分:2)
$skuArray = array('00240'=>123,'00241'=>456);
$keys = array_keys($skuArray);
$getSkus = mysql_query("SELECT sku FROM data WHERE sku IN('" . join("','", $keys) . "')");
答案 1 :(得分:2)
试试这个:
<?php
$skuArray = array('00240'=>123, '00241'=>456);
$inSkus = array();
foreach (array_keys($skuArray) as $key)
{
$inSkus[] = '"' . $key . '"';
}
$sql = 'SELECT sku FROM data WHERE sku IN (' . implode(', ', $inSkus) . ')';
echo $sql;
您需要将密钥作为字符串,然后需要将它们包装在SQL查询的括号中。
答案 2 :(得分:0)
foreach($skuArray as $value=>$key){
if($where=="")
$where= $value" = '".$key."'";
else
$where.= $value" = '".$key."'";
}
$getSkus = mysql_query("SELECT sku FROM data " .($where)?" WHERE $where )":"");