简体   繁体   English

从PHP中准备好的MySQL语句的结果计算唯一值

[英]Count unique values from the results of a prepared MySQL statement in PHP

I have an online calendar listing upcoming live music. 我有一个在线日历,列出即将到来的现场音乐。 I use a prepared statement to fetch the listings for the next eight days and display them in a table. 我使用准备好的语句来获取接下来八天的列表并将它们显示在表格中。 What I need to do, before displaying the results, is count the number of unique dates ('Date') within the listings. 在显示结果之前,我需要做的是计算列表中唯一日期(“日期”)的数量。 For example, if only five days out of the next eight have events happening, I need to know that number is 5. 例如,如果接下来八天中只有五天发生事件,那么我需要知道数字是5。

Using COUNT(DISTINCT) works for giving me that number, but then it only displays one row of results, so I need another solution 使用COUNT(DISTINCT)可以给我这个数字,但是它只显示一行结果,所以我需要另一个解决方案

My code is this: 我的代码是这样的:

$mysqli = new mysqli("Login Stuff Here");
if ($mysqli->connect_errno){
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$start = strtotime('today midnight');
$stop = strtotime('+1 week');

$start = date('Y-m-d', $start);
$stop = date('Y-m-d', $stop);

$allDates = $mysqli->prepare("SELECT 
    ID,
    Host,
    Type,
    Bands,
    Date,
    Time,
    Price,
    Note,
    Zip,
    URL
    FROM things WHERE (Date >= ? AND Date <= ?) ORDER BY Date, Time, Host");
$allDates->bind_param("ss", $start, $stop);
$allDates->execute();
$allDates->bind_result($ID, $Host, $Type, $Bands, $Date, $Time, $Price, $Note, $Zip, $URL);

while($allDates->fetch()):
  // ECHO ALL THE INFO IN A NICE TABLE
  endwhile;

$allDates->close();

I need to count the unique values (and maybe even retrieve them) from the 'Date' column. 我需要计算“日期”列中的唯一值(甚至可能检索它们)。 Right now I have it working by doing a separate query, but I'm sure there's a better way. 现在,我可以通过执行单独的查询来使其正常工作,但是我敢肯定有更好的方法。

EDIT: Ultimately, I wound up doing a separate query, which worked out well as I was able to use it for other things as well. 编辑:最终,我结束了做一个单独的查询,效果很好,因为我也可以将其用于其他事情。 I found that using GROUP BY always only returned just one result per date, so it didn't work for displaying the full listings. 我发现使用GROUP BY在每个日期总是只返回一个结果,因此对于显示完整列表不起作用。 Maybe I was going at it wrong, but I wound up being good in another way. 也许我做错了,但我最终以另一种方式成为好人。 Thanks! 谢谢!

您缺少分组依据:

GROUP BY Date

To use any aggregate method like count, sum for specific group. 要使用任何总计方法(如count,特定组的总和)。 You need to apply GROUP BY on specific column or list of columns. 您需要在特定列或列列表上应用GROUP BY

Example : 范例:

 GROUP BY Date ORDER BY Date, Time, Host

Please note that, list of columns in SELECT MUST match the list of columns mentioned along with GROUP BY . 请注意, SELECT中的列列表必须GROUP BY匹配。

Also, 也,

Date >= ? AND Date <= ?

can be replaced by 可以替换为

Date BETWEEN ? AND ?

I'd recommend grouping by date as you fetch the results from the query. 当您从查询中获取结果时,建议您按日期分组。

while($row = $allDates->fetch()) {
    $dates[$row['date'][] = $row;
}

That makes it easy to count the distinct dates 这样可以轻松计算出不同的日期

$count = count($dates);

And potentially simpler to format your output (headers/sections for each date, etc.) 可能更简单地格式化输出(每个日期的标题/节等)。

foreach($dates as $date) {
    foreach($date as $event) {
        // ECHO ALL THE INFO IN A NICE TABLE
    }
}

It does require iterating the same data twice, but for a reasonable amount of data to display on a page, that shouldn't make much difference. 它确实需要重复两次相同的数据,但是要使相当数量的数据显示在页面上,应该没有太大的区别。

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

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