[英]MySQL Select where two fields are in multidimensional array
I have the following multidimensional array, generated after some database Selects: 我有以下多维数组,是在某些数据库选择后生成的:
<?php
$array = array (
array("X"=>500, Y="400"),
array("X"=>234, Y="347"),
array("X"=>845, Y="345"),
array("X"=>264, Y="916")
);
?>
Now I need to do a select in a table where BOTH field X and Y are in the array. 现在,我需要在表中X和Y都在数组中的表中进行选择。 How to do that? 怎么做? Like: 喜欢:
SELECT FROM table WHERE
(X=500 AND Y=400) OR
(X=234 AND Y=347) OR
(X=845 AND Y=345) OR
(X=264 AND Y=916)
;
I can only find solutions here on StackOverflow for a single item in a query, but not for two values in a multidimensional array that needs to be exactly the same. 我只能在StackOverflow上为查询中的单个项目找到解决方案,而对于多维数组中需要完全相同的两个值找不到解决方案。 Thank you! 谢谢!
If I understood, you need to read your array and pass it to your SQL. 据我了解,您需要读取数组并将其传递给SQL。
Something like: 就像是:
<?php
$where = null;
foreach ($array as $a)
{
if (is_null($where))
{
$where = " WHERE (X = " . $a['X'] . " AND Y = " . $a['Y'] . ")";
}else
{
$where .= " OR (X = " . $a['X'] . " AND Y = " . $a['Y'] . ")";
}
}
$sql = "SELECT * FROM table " . $where;
?>
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.