使用PHP计算数据库中的特定值

时间:2016-10-06 05:24:44

标签: php mysql laravel laravel-5.2

看一下我的疑问:

<?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 categoryName表中有categoriesFull Time。 说它的返回3.并且这3个是全职,兼职和合同。现在我想计算categories表中有多少SELECT t.Name, t.Country, t.Status, t.Total DENSE_RANK() OVER (PARTITION BY t.Name ORDER BY t.Total DESC, t.Country) AS rn FROM ( SELECT Name, Country, Status, COUNT(*) AS Total FROM table1 GROUP BY Name, Country, Status ) t WHERE rn <= 3 个数据。如果您需要任何进一步的信息,请告诉我问题

1 个答案:

答案 0 :(得分:2)

您可以通过在查询中添加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,上面的例子可能会有一些语法错误。