简体   繁体   English

MySQL计数行数取决于日期

[英]Mysql Count number of Rows Depending on Date

Well, I don't know what title it should be nor how can I explain it, but I'll do my best to explain my problem. 好吧,我不知道它应该是什么标题,也不知道如何解释它,但我会尽力解释我的问题。

I have this table consists of certain data that focuses on date_participated and fblike_point . 我有这个表包含一些专注于date_participatedfblike_point的数据

在此输入图像描述

Well, my desired output should be 好吧,我想要的输出应该是

2013-03-20 1 2013-03-20 1

2013-03-19 3 2013-03-19 3

So basically, each date will count the number of fblike_point . 因此,基本上, 每个日期都会计算fblike_point的数量

On 2013-03-19 it has total of 3 fblike_point and on 2013-03-20 it has a total of 1 fblike_point 在2013-03-19它总共有3个fblike_point ,在2013-03-20它总共有1个fblike_point

and my problem is, I'm outputting not my desired results, I'm getting a result of 2013-03-19 4 我的问题是,我没有输出想要的结果,我得到了2013-03-19 4的结果

It stays in 1 date and counts the number of fblike_point 它停留在1个日期并计算fblike_point的数量

Here is my code for that. 这是我的代码。

$query = "SELECT DISTINCT date_participated, COUNT(fblike_point) as fblike FROM event_raffles WHERE ticket_id='". $ticket_id ."' AND event_table='". $event_table ."' AND status='1' ORDER BY date_participated DESC";
$result = db_query($query);

foreach ($result as $values) {
    echo $values->date_participated . " " . $values->fblike . "<br>";
}

Any solutions for this stuff? 有什么解决方案吗? Any help would be appreciated. 任何帮助,将不胜感激。 :) :)

You should alter your query to use a GROUP BY instead of the DISTINCT 您应该更改查询以使用GROUP BY而不是DISTINCT

SELECT date_participated, COUNT(fblike_point) as fblike 
FROM event_raffles
GROUP BY date_participated

The way you've used DISTINCT is to get all unique dates and for each result, add the amount of records returned in the entire resultset. 您使用DISTINCT是获取所有唯一日期,并为每个结果添加整个结果集中返回的记录数量。

Using a GROUP BY allows you to use any aggregate function available on that group . 使用GROUP BY允许您使用该组上可用的任何聚合函数。


Edit 编辑

As been mentioned by @ysth, instead of using COUNT you might want to use SUM depending on the possible values that fblike_point can have. 正如@ysth所提到的,您可能希望使用SUM取决于fblike_point可能具有的值,而不是使用COUNT

Using SUM would actually be the more sensible course of action. 使用SUM实际上是更明智的行动方案。

use below query by removing DISTINCT and replacing ORDER BY to GROUP BY 通过删除DISTINCT并将ORDER BY替换为GROUP BY来使用以下查询

$query = "SELECT date_participated, COUNT(fblike_point) as fblike FROM event_raffles WHERE ticket_id='". $ticket_id ."' AND event_table='". $event_table ."' 
AND status='1' GROUP BY date_participated DESC";

Replace 更换

$query = "SELECT DISTINCT date_participated, COUNT(fblike_point) as fblike FROM event_raffles WHERE ticket_id='". $ticket_id ."' AND event_table='". $event_table ."' AND status='1' ORDER BY date_participated DESC";

with

$query = "SELECT  date_participated, COUNT(fblike_point) as fblike FROM event_raffles WHERE ticket_id='". $ticket_id ."' AND event_table='". $event_table ."' AND status='1' GROUP BY date_participated ORDER BY date_participated DESC";

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

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