我希望能够只获取在多列中有'1‘的值。不过,我的列名是动态存储在变量中的。我该怎么做呢?
这就是我拥有的。列的布局如下
$Columns = "Computer, Science, Algebra, Biology, Networking";
//This is Dynamic so $Columns may be like this next time
$Columns = "Biology, Networking";
$SQL = "SELECT * FROM users WHERE '1' IN(".$Columns.")";
现在,它选择在任何列中具有"1“的任何用户
我只想检索数组$Columns中变量的ALL为"1“的用户,而不只是其中的一个
发布于 2018-06-25 10:38:31
由于您的$columns变量可以有不同的大小,因此您需要基于for循环中数组中的列数来构建sql语句:
// SET your first column IN
$sql = "SELECT * FROM users WHERE '1' IN (".$Columns(0).")"
//Run the for loop to build sql on all subsequent columns
// size of will give you the number of columns in your array
$max = sizeof($columns);
for($i = 1; $i < $max;$i++)
{
//Use the "AND" to make sure "1" is in all columns
$sql.=" AND '1' IN (".$Columns($i).")
}
$sql.=";"我的代码可能并不完美,但应该能帮助您找到正确的方向。
https://stackoverflow.com/questions/51015316
复制相似问题