count-specific-value在php数据库中的应用

46qrfjad  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(252)

看看我的问题:

<?php
  $db = mysqli_connect("localhost", "root", "", "dbname");
  $query = mysqli_query($db,"select count(categoryName) as total from categories");
  $result = mysqli_fetch_array($query);
  echo $result['total'];
?>

此查询是 count 有多少 categoryNamecategories table。说它的回报3。这三个是全职,兼职和合同制的。现在我想数一数有多少 Full Time 数据库中的数据 categories 如果你需要这个问题的更多信息,请告诉我。

pu82cl6c

pu82cl6c1#

可以通过添加 where 你的问题。举个例子:

mysqli_query($db,"select count(categoryName) as total from categories where categoryName = 'Full Time'");

为了响应op的注解,您可以获得列中具有相同值的行的计数 categoryName 使用这样的查询:

SELECT categoryName AS name, count(*) AS total FROM categories GROUP BY categoryName;

例子:

$db = mysqli_connect("localhost", "root", "", "dbname");
$query = mysqli_query($db,"SELECT categoryName AS name, count(*) AS total FROM categories GROUP BY categoryName;");
while($result = mysqli_fetch_array($query)){
    echo $result['name'] . " " . $result['total'];
}

我已经有一段时间没写php了,上面的例子可能有一些语法错误。

相关问题