简体   繁体   English

PHP计算在一列中包含相同值的MYSQL行

[英]PHP counting MYSQL rows that contain the same value in one column

I have a database and there is the column named 'status'. 我有一个数据库,有一个名为'status'的列。 This column could contain five possible values. 此列可能包含五个可能的值。 Now I need to sum number of rows, where the status column contains value 'canceled'. 现在我需要对行数求和,其中status列包含值'cancelled'。 What function in the PHP I should use to count it, please? 我应该使用PHP中的哪些函数来计算它? Thanks for response. 谢谢你的回复。


EDIT: 编辑:

I think here's the complete solution. 我认为这是完整的解决方案。 Correct me if I'm wrong. 如果我错了纠正我。

$canceledsum=mysql_query("SELECT COUNT(*) AS totalcanceled FROM nabidka WHERE status='canceled'");
$d=mysql_fetch_assoc($canceledsum);
echo $d['totalcanceled'];

You are probably looking for this: 你可能正在寻找这个:

SELECT COUNT(*)
FROM yourtable
WHERE status='canceled'

or this query that will count all rows for all different status values: 或者此查询将计算所有不同状态值的所有行:

SELECT status, COUNT(*)
FROM yourtable
GROUP BY status

Edit: if you want to count only records created on the current month: 编辑:如果您只想计算当前月份创建的记录:

SELECT status, COUNT(*)
FROM yourtable
WHERE create_date >= DATE_FORMAT(CURDATE(), '%Y-%m-01')
GROUP BY status

here DATE_FORMAT(CURDATE(), '%Y-%m-01') will return the first day of the current month, if you don't have records created in the future it will be fine. 这里DATE_FORMAT(CURDATE(), '%Y-%m-01')将返回当月的第一天,如果您将来没有创建记录就可以了。

Instead of using PHP to count, you might directly use sql. 您可以直接使用sql,而不是使用PHP来计算。

Fire this query: 触发此查询:

SELECT COUNT(*) FROM table_name WHERE status = 'canceled';

As for how to fire queries and getting data from PHP, please refer this link 至于如何触发查询和从PHP获取数据,请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM