如何按年份降序和月份订购表?

时间:2015-09-09 16:21:17

标签: php mysql

我试图从mysql数据库构建一个html表。一年中的几个月将是最重要的一年(1月至12月),这些年将是左边的第一列,2015年是顶部,2008年是底部。目前我按日期订购,降序。这意味着最新项目将位于左上角位置。但是,我需要将它按ASC分类一年中的几个月,以及多年来的DSC。我假设我需要使用DATE_FORMAT,但我不确定如何实现这一点。数据库中的日期格式如下:2014-07-01

            $resource_where_conditions = '`publish` = \'y\' and `category_id` = ' . $cat_data['id'];
            $date_result = $db->selectByStrings($resource_data_fields, '`resource`', $resource_where_conditions, '`date` desc');
            $current_year = '';
            $base_url_query = (_USE_SEO_URLS === true) ? $_SERVER['PHP_SELF'] . '?' : $_SERVER['REQUEST_URI'] . '&';
            if ($req->isRequest ('year')) $url_year = $req->getRequest ('year');
            if ($db->getNumRows($date_result) > 0)  {
                $copy_investor_newsletter .= $tab0 . '<table class="table table-bordered table-responsive"><tbody><thead><tr><th>January</th><th>Feburay</th><th>March</th><th>April</th><th>May</th><th>June</th><th>July</th><th>August</th><th>September</th><th>October</th><th>November</th><th>December</th></tr></thead>' . $retn;
                while ($data = $db->getNextRow($date_result))   {
                    if (substr ($data['date'], 0, 4) != $current_year)  {
                        $current_year = substr ($data['date'], 0, 4);
                        if (!isset ($latest_year)) $latest_year = $current_year;
                        if (!isset ($url_year)) {
                            $navsel = ' class="ActNav"';
                            $url_year = $current_year;
                        } elseif ($url_year == $current_year)   {
                            $navsel = ' class="ActNav"';
                            $selected_year = $url_year;
                        } else $navsel = '';
                        $copy_investor_newsletter .= $tab1 . '<tr>';
                //      if ($url_year == $current_year) {
                            /*
                             * read and display all newsletters for selected year
                             */
                            $resource_where_conditions = '`publish` = \'y\' and `category_id` = ' . $cat_data['id'] . ' and substr(`date`,1,4) = \'' . $current_year . '\'';
                            $resource_result = $db->selectByStrings($resource_data_fields, '`resource`', $resource_where_conditions, '`date` desc');
                            if ($db->getNumRows($resource_result) > 0)  {
                        //      $copy_investor_newsletter .= $retn . $tab2 . '<ul>' . $retn;
                                while ($data = $db->getNextRow($resource_result))   {
                                    $copy_investor_newsletter .= $tab3 . '<td><a href="/includes/act_download.php?fid=' . $this_fund_id . '&rid=' . $data['id'] . '&cid=' . $cat_data['id'] . '&download=' . $data['file'] . '" target="_blank">' . $data['heading'] . '</a></td>' . $retn;
                                }
                            //  $copy_investor_newsletter .= $tab2 . '</ul>' . $retn . $tab1;
                            }
            //          }
                        $copy_investor_newsletter .= '</tr>' . $retn;
                    }
                }
                $copy_investor_newsletter .= $tab0 . '</tbody></table>' . $retn . $retn;
            }

2 个答案:

答案 0 :(得分:3)

尝试此SQL查询以获得所需的排序:

SELECT * FROM Table ORDER BY YEAR(date) DESC, MONTH(date) DESC

答案 1 :(得分:0)

不完全明白,你现在拥有的和你想要的之间有什么区别

但我认为你应该朝着这个方向看:

select date_format(date, '%c') month, date_format(date, '%Y') year, id, name, etc
    from ... order by month ASC, year DESC

因此,您在数字演示文稿中选择月份,然后您可以通过它进行排序。