我有一个PHP数组:Array(keyword1,blah0,blah1,blah2)
和一个SQL查询。
在查询中,我想查找Array中的一个数据是否存在于所选(columnA|columnB|columnC)
的少数列之一中,如果为true,则获取该行。
您需要这样的查询:
$instring = implode("','", $array);
$sql = "SELECT `xy` FROM `z` WHERE (`columnA` IN ('".$instring."')
OR `columnB` IN ('".$instring."')
OR `columnC` IN ('".$instring."'))";
我想这就是你想要的。我不知道是否要检查数组中每一列的每一个值。
SELECT * FROM tableName
WHERE columnA = $phpArray[0] OR
columnA = $phpArray[0] OR
columnB = $phpArray[0] OR
columnC = $phpArray[0] OR
columnA = $phpArray[1] OR
columnB = $phpArray[1] OR
columnC = $phpArray[1] OR
columnA = $phpArray[2] OR
columnB = $phpArray[2] OR
columnC = $phpArray[2] OR
...
columnC = $phpArray[n];
您可以使用PHP在循环中创建此语句
$sql = "SELECT * FROM tableName WHERE ";
for($i=0;$i<count(phpArray)-1;$i++){
$sql .= "columnA = "+"'"+$phpArray[$i]+"'"+ OR "
$sql .= "columnB = +"'"+$phpArray[$i]+"'"+ OR "
$sql .= "columnC = +"'"+$phpArray[$i]+"'"+ OR "
}
$sql .= "columnA = +"'"+$phpArray[$i]+"'"+ OR "
$sql .= "columnB = +"'"+$phpArray[$i]+"'"+ OR "
$sql .= "columnC = +"'"+$phpArray[$i]+"'"+;"
*send sql query
编辑:修复代码中的错误