我有一个客户表,在不同的日期记录了他们的客户I为1。我想找出按降序记录的1的总和。我用的是MySQL和php谢谢
发布于 2010-03-12 21:46:05
我的猜测是,您希望每个客户的记录总和标记为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这里假设'one‘是包含1的列(并且只包含0或1-否则您必须添加cone.one =1的位置),customers是您的customer表,customer_ones是包含您的客户数据的表。
发布于 2010-03-12 20:36:59
根据我的理解,这是您需要的简单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_num_rows。
或者更准确地解释一下你需要什么输出。要按日期或任何其他依赖项进行排序,您将需要使用WHERE子句和日期比较发出其他请求。
https://stackoverflow.com/questions/2432570
复制相似问题