gpt4 book ai didi

mysql - 我应该按月比较学生

转载 作者:行者123 更新时间:2023-11-29 21:53:42 25 4
gpt4 key购买 nike

public function getComparisonStudentsByMonth()
{
$last_month = date('m', strtotime('-2 month'));
$previus_month = date('m', strtotime('-1 month'));

return
$this->db->select('MONTH('student_add_date')', date('m'));
$this->db->where('student_add_date <= DATE_ADD(NOW(),INTERVAL 30 DAYS)');
$query = $this->db->get('students')->result();
}

问题

用户每月插入新学生和管理页面应显示月份之间的百分比比较。我应该把上个月当作 100% 并与当月进行比较。我理解逻辑,但是如何使用 MySql。

最佳答案

要获取每个月的所有百分比,您可以使用以下命令:

SELECT (MONTH(student_add_date)) AS month_temp, ((count(*) /(SELECT count(*) FROM students)) * 100)percentage from students group by month_temp

要获取两个月之间的数据,您需要在查询中指定月份。例如我将在九月(9)和十月(10)进行搜索

SELECT 
(MONTH(student_add_date)) AS month_temp,
((count(*) /(SELECT count(*) FROM students WHERE MONTH(student_add_date) = 9 OR MONTH(student_add_date) = 10)) * 100)percentace
from students
WHERE
MONTH(student_add_date) = 9
OR
MONTH(student_add_date) = 10
group by month_temp

在 codeigniter 中,您可以像这样运行自定义查询:

$query = $this->db->query('SELECT 
(MONTH(student_add_date)) AS month_temp,
((count(*) /(SELECT count(*) FROM students WHERE MONTH(student_add_date) = 9 OR MONTH(student_add_date) = 10)) * 100)percentace
from students
WHERE
MONTH(student_add_date) = 9
OR
MONTH(student_add_date) = 10
group by month_temp');


$result = $query->result_array();

// var_dump($result);

return $result;

关于mysql - 我应该按月比较学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com