简体   繁体   English

MySQL查询–有更好的方法吗?

[英]MySQL Query – is there a better way?

On my blog I am have a function that counts how many blog posts have been made month by month and these can be displayed in a sub menu. 在我的博客上,我具有一个功能,该功能可以统计每月发布的博客帖子数量,这些帖子可以显示在子菜单中。

It is looped by year, and then another nested loop by month. 它按年循环,然后按月循环另一个嵌套。 Each time it runs this query to count how many articles were posted in each month. 每次运行此查询以计算每个月发布了多少篇文章。

This is the query: 这是查询:

Select * from DM_blog blog where  blog.id IN 
    ( SELECT entry_id FROM DM_tags_target tagstarget WHERE tagstarget.parent_id IN 
    ( SELECT id FROM DM_tags tags WHERE tags.tag = '".$data['page']['id']."' AND tags.type = 'blog_target' )) AND blog.publish < '".date("Y-m-d H:i:s")."' and blog.status = '0'  and YEAR(blog.publish) = '".$year."' and MONTH(blog.publish) = '".$month."'"

Is there a better way to do this? 有一个更好的方法吗? Can I do all of the months at once instead of having to do 12 queries a year? 我可以一次完成所有月份,而不必每年进行12个查询吗? The full code is here: 完整的代码在这里:

$this->benchmark->mark('blog_submenu_start');
    $dates = $this->blog_model->menu_dates($data);
    if($dates['years']){
        $i = 0;
        foreach($dates['years'] as $year){
            if($data['segments']['modal'] == $year['YEAR(blog.publish)']){
                $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['class'] = 'selected';
                $selected = true;
            }else if(date("Y") == $year['YEAR(blog.publish)'] && $selected == false){
                $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['class'] = 'selected';
                $selected = true;
            }

            // find the start month
            if($dates['first_entry'][0]['YEAR(blog.publish)'] == $year['YEAR(blog.publish)']){
                if($dates['first_entry'][0]['MONTH(blog.publish)'] < 2){
                    $limit_s['start'] = '1';
                    $limit_s['end'] = 13;
                }else{
                    $limit_s['start'] = $dates['first_entry'][0]['MONTH(blog.publish)'];
                    $limit_s['end'] = 12;
                }
            }else{
                $limit_s['start'] = 1;
                $limit_s['end'] = 13;
            }
            // run through the months
            $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_start');
            for($x=$limit_s['start'];$x<=$limit_s['end'];$x++){
                $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_start');
                $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_mysql_start');
                $count = $this->blog_model->month_count($year['YEAR(blog.publish)'],$x,$data);
            $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_mysql_end');
                if($last == false){
                    $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_selected_start');
                    if($data['segments']['modal'] == $year['YEAR(blog.publish)'] and $data['segments']['para_one'] == $x){
                        $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['class'] = 'selected';
                    }
                    $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_selected_end');
                    $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_assign_start');
                    $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['month'] = $x;
                    $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['display_full'] = date('F',strtotime($year['YEAR(blog.publish)'].'-'.$x.'-01')); 
                    $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['display_short'] = date('M',strtotime($year['YEAR(blog.publish)'].'-'.$x.'-01')); 
                    $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['count'] = $count;
                    $data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['sef'] = $year['YEAR(blog.publish)'].'/'.$x.'/';
                    $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_assign_end');
                    if(date("Y") == $year['YEAR(blog.publish)'] and date("m") == $x || date("n") == $x){
                        $last = true;
                    } // end date
                    $i++;
                    $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_end');
                } // end last false
            } // end for
            $this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_end');
        }
    }
    $this->benchmark->mark('blog_submenu_end');

$this->blog_model->month_count - this function is as follows: $ this-> blog_model-> month_count-此函数如下:

function month_count($year,$month,$data=false){
    $query = $this->db->query("Select * from DM_blog blog where  blog.id IN 
    ( SELECT entry_id FROM DM_tags_target tagstarget WHERE tagstarget.parent_id IN 
    ( SELECT id FROM DM_tags tags WHERE tags.tag = '".$data['page']['id']."' AND tags.type = 'blog_target' )) AND blog.publish < '".date("Y-m-d H:i:s")."' and blog.status = '0'  and YEAR(blog.publish) = '".$year."' and MONTH(blog.publish) = '".$month."'");
    return $query->num_rows(); 
}

You can add group by clause to group the results by year and month. 您可以添加group by子句以按年和月对结果进行分组。

Select * from DM_blog blog where  blog.id IN 
( SELECT entry_id FROM DM_tags_target tagstarget WHERE tagstarget.parent_id IN 
( SELECT id FROM DM_tags tags WHERE tags.tag = '".$data['page']['id']."' AND tags.type =     'blog_target' )) AND blog.publish < '".date("Y-m-d H:i:s")."' and blog.status = '0'  GROUP BY     YEAR(blog.publish), MONTH(blog.publish)

You could use a GROUP BY expression: 您可以使用GROUP BY表达式:

SELECT id, YEAR(blog.publish), MONTH(blog.publish)
  FROM DM_blog blog 
 WHERE  blog.id IN 
( SELECT entry_id 
    FROM DM_tags_target tagstarget 
   WHERE tagstarget.parent_id IN 
         ( SELECT id 
             FROM DM_tags tags
            WHERE tags.tag = '".$data['page']['id']."' 
              AND tags.type = 'blog_target' 
          )
) 
AND blog.publish < '".date("Y-m-d H:i:s")."' 
AND blog.status = '0'  
GROUP BY YEAR(blog.publish), MONTH(blog.publish)

It will give you the results grouped by year and month 它将为您提供按年和月分组的结果

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

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