当我在mysql中有值和空值时有人可以帮助我rowCount()
例如:
id column1 column2 idcolumn
1 2 1
2 3 1
3 1 1
4 0 1
$CountQuery = "SELECT column1 FROM follow WHERE idcolumn= ?";
$Count = $dbh->prepare($CountQuery);
$Count -> execute(array($idcolumn)); //where $idcolumn = 1
$num_1 = $Count->rowCount();
echo $num_1;
这应该给我一个4的计数,但是我试图区分column1和column2,所以我可以找到计数,如果我想用$ num_2创建一个查询,它应该给我一个0。
最佳答案
使用COUNT(column)
计算该列中具有非NULL值的行数。
$CountQuery = "SELECT COUNT(column1) AS `count1`, COUNT(column2) AS `count2` FROM follow WHERE idcolumn = ?";
$Count = $dbh->prepare($CountQuery);
$Count->execute(array($idcolumn)); //where $idcolumn = 1
$row = $Count->fetch(PDO::FETCH_ASSOC);
$num_1 = $row['count1'];
$num_2 = $row['count2'];