简体   繁体   English

如何在 php mysql 中使用“AND”查询获得多于一行

[英]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.

相关问题 如何通过一个查询使用PHP获取MySQL字段? - How can I get the MySQL fields using PHP with one query? 我如何使用 PHP 和 MYSQL 数据库在我的表中显示多行 - How can i display more than one row in my table USING PHP & MYSQL database 如何获取包含一个或多个关键字的页面列表-MySQL和PHP - How can I get a list of pages that contain one or more keyword - MySQL and PHP 如何通过一个查询或PHP在MySQL中获取所需的数据? - How to get the data I want in MySQL in one query or in PHP? 我如何用php创建比此更好的mysql查询 - How can I create a better mysql query with php than this one 如何编写一个查询来获取mysql中的计数 - how can i write as one query to get count in mysql 如果使用一个查询而不是嵌套的复选框选中了复选框,是否可以向表中添加新行? PHP,MySQL - Can I add a new row to a table if a checkbox is checked using one query rather than nested? PHP, MySQL 如何使用php从MySQL查询中获取索引值? - How can i get the index value from the MySQL query with php? 我的php代码中有一个mysql查询,该查询被剪切并粘贴在两个地方。 我如何才能将它放在一个地方? - I have a mysql query in my php code that is cut and pasted in two places. How can I get it to be in one place? 在 MySQL 数据库中,如何为通过 php 和 json 连续重复多次的字段创建不同的键 - In MySQL database how can I create a different key for a field that repeats more than once in a row by php and json
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM