[英]How can I get more then one row with “AND” query in php mysql
id - idProduct - idFilter
65 - 494 - 3
66 - 495 - 3
67 - 497 - 17
68 - 500 - 17
69 - 500 - 3
How I can get all idProducts who have idFilter 17 AND 3. In this case this is only product with id - 500我如何获得所有具有 idFilter 17 和 3 的 idProducts。在这种情况下,这只是 id - 500 的产品
I try something like this: $query="SELECT idProduct FROM tablename WHERE (idFilter = '3') OR (idFilter = '17') GROUP BY idProduct";我尝试这样的事情: $query="SELECT idProduct FROM tablename WHERE (idFilter = '3') OR (idFilter = '17') GROUP BY idProduct";
I know it's stupid but I don't know any idea?我知道这很愚蠢,但我不知道任何想法?
Thank you谢谢
In case if you need to compare multiple row values on a single column, Such that the value should satisfy with an ANS condition.如果您需要比较单个列上的多个行值,则该值应满足 ANS 条件。
Then you can do the following trick to achieve that:然后,您可以执行以下技巧来实现这一目标:
SELECT distinct(idProduct) as idProduct,count(*) AS total FROM tablename
WHERE idFilter IN (17,3) GROUP BY idProduct
HAVING total = 2;
//Having will force result to have both 17 and 3 else 17 or 3 alone will be in the result
If using php variable for the above ids, you can try the following如果对上述 id 使用 php 变量,您可以尝试以下操作
$inArray = array(17,3);
$sql = "SELECT distinct(idProduct) as idProduct,count(*) AS total FROM tablename
WHERE idFilter IN (".implode(',',$inArray).") GROUP BY idProduct
HAVING total = '".count($inArray)."'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.