显示最常出现的日期

时间:2014-11-30 15:02:50

标签: php mysqli

此脚本返回发布大多数笑话的日期以及笑话的数量。 问题是当有两个或更多日期具有相同数量的笑话时。 当这种情况发生时如何显示?

当有两个或更多日期具有相同数量的笑话时,我想回显所有日期。类似的事情:有更多的日子有相同数量的笑话(显示日期)这些日子里发布的笑话是(笑话的数量)

<?php
 $page_title = 'day with most jokes';
include ('includes/header.html');
require ('mysqli_connect.php');


$q="SELECT date, COUNT(date) AS value_occurrence FROM joke GROUP BY date ORDER BY value_occurrence DESC LIMIT";
$r = mysqli_query($dbc, $q) or die(mysql_error());
$row=  mysqli_fetch_row($r);


echo'day <b>' . $row[0] . '</b>is the day with most joke(s) </br>' ;
echo 'on that day there were<b> ' . $row[1] . ' <b> joke(s)';


    ?>

1 个答案:

答案 0 :(得分:0)

我会将查询更改为:

SELECT * FROM
    (SELECT date, COUNT(date) AS value_occurrence, MAX(value_occurence) AS max_occ
     FROM joke
     GROUP BY date
     ORDER BY value_occurrence DESC)
WHERE value_occurence = max_occ

或那:

SELECT date, value_occurence, MAX(value_occurence) AS max_occ FROM
    (SELECT date, COUNT(date) AS value_occurrence
     FROM joke
     GROUP BY date
     ORDER BY value_occurrence DESC) sq
WHERE value_occurence = max_occ

然后在PHP代码中,我会估计返回的行数并相应地显示日期。