Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在我的表中按顺序查找1的数量_Php_Mysql_Sum_Sql Order By - Fatal编程技术网

Php 在我的表中按顺序查找1的数量

Php 在我的表中按顺序查找1的数量,php,mysql,sum,sql-order-by,Php,Mysql,Sum,Sql Order By,我有一个客户表,在不同的日期根据客户ID记录了1。 我想找到按降序记录的1的总和。我正在使用MySQL和php 谢谢我理解正确,这是您需要的简单sql请求: SELECT COUNT(id) as total from customers 只需在php中制作: $sql="SELECT COUNT(id) from customers"; $query=mysql_query($sql) or die(mysql_error()); $res=mysql_fetch_

我有一个客户表,在不同的日期根据客户ID记录了1。 我想找到按降序记录的1的总和。我正在使用MySQL和php
谢谢

我理解正确,这是您需要的简单sql请求:

SELECT COUNT(id) as total from customers
只需在php中制作:

   $sql="SELECT COUNT(id) from customers";
    $query=mysql_query($sql) or die(mysql_error());
      $res=mysql_fetch_assoc($query);

 $summ=$res['total'];   //<- Your summ (i.e. quantity of rows in table)
顺便说一句,您可以使用mysql\u num\u行


或者请更准确地解释您需要什么输出。要按日期或任何其他依赖项进行排序,您需要使用WHERE子句和日期比较进行其他请求。

我猜您希望记录的总和标记为每个客户1,并按降序排序?如果是这样的话,以下几点可以做到:

select cust.id, sum(cone.one) as number_ones 
from customers as cust
inner join customer_ones as cone on cone.id=cust.id
group by cust.id
order by number_ones desc

这是假设“一”是包含一的列,只包含0或1,否则您必须添加cone.one=1,customers是您的customer表,customer_是包含您的customer数据的表。

无论您添加顺序如何,1的总和都是相同的。你能编辑你的问题来更好地解释它吗?为什么在你返回一笔金额时订单会起作用?对不起,新手错误,尽管我已经阅读了很多次,但我避免提问,因为我知道我无法正确解释它。我想列出记录数量最多的客户,比如排名前十的ie客户46有10个1,客户36有9个1,等等,感谢您提供了包含php的详细信息