我的要求是获取行数。在我的MySQL中,我有一个列(项目),其值为" 1265855"。当我得到计数时,我需要忽略列1265855
中item
的行。我怎样才能在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();
答案 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'";