跳过具有指定值的行

时间:2016-03-21 05:02:58

标签: php pdo

我的要求是获取行数。在我的MySQL中,我有一个列(项目),其值为" 1265855"。当我得到计数​​时,我需要忽略列1265855item的行。我怎样才能在sql语句中执行此操作?

$sql = "SELECT COUNT(*) as count FROM stock WHERE availability = :availability  AND item = :item";
$stement = $con->prepare($sql);
$stement->execute(array(':availability'=> $val1, ':item' => $val2)); // ignore 1265855
$result = $stement->fetchAll();

2 个答案:

答案 0 :(得分:0)

假设您没有尝试过滤该项目,这应该有效:

$sql = "SELECT COUNT(*) as count 
          FROM stock 
         WHERE availability = :availability  
           AND item != :item";

$stement = $con->prepare($sql);
$params = array(':availability'=> $val1, ':item' => '1265855'); 
$stement->execute($params); // ignore 1265855
$result = $stement->fetchAll();

不确定你用$val2做了什么,否则就是

答案 1 :(得分:0)

您可以在查询中添加<>

<> 称为“不等于”,当您在查询中排除某行时,也可以使用此选项。请注意,在某些版本的SQL中,此运算符可能写为!=

以下是如何在当前代码中应用它:

$sql = "SELECT COUNT(*) as count FROM stock WHERE availability = :availability AND item <> 1265855";

$sql = "SELECT COUNT(*) as count FROM stock WHERE availability = :availability AND item != 1265855";

表示字符串:

$sql = "SELECT COUNT(*) as count FROM stock WHERE availability = :availability AND item != 'string'";