如何分别计算一列中的值取决于 codeigniter 中的值?
How to count value from one column separately depends on value in codiegniter?
这是我的 table,我想从这个 table 中计算 attendance_status_id
,其中年份 1399
和 attendance_status_id
值相同。例如,在这个 table 中,两个 attendance_status_id
具有相同的值 1
,所以当我将它算作 present_status
而另一个 2
算作 absent_status
我该怎么做?
这是我的功能:
public function get_student_attendance_daily_report($data)
{
return $this->db->select('sda.id sda_id,st_id,student.student_id,student.name,last_name,fname,school_class_fa_name,
year,COUNT(attendance_status_id) as at_s_id,attendance_status_id,date,description')
->from('student_daily_attendance sda')
->join('student', 'student.st_id=sda.student_id')
->join('school_class', 'school_class.school_class_id=sda.school_class_id')
->join('jalali_months', 'jalali_months.id=sda.month_id')
->where('sda.year', $data['edu_year'])
->where('sda.school_class_id', $data['grades'])
->where('sda.class_id', $data['class'])
->order_by('sda.id','desc')
->group_by('sda.student_id,sda.attendance_status_id')
->get()->result_array();
}
结果如下:
array (
'Total' => 36,
)array (
0 =>
array (
'sda_id' => '85',
'st_id' => '858',
'student_id' => 'S99888',
'name' => 'ٌsafa',
'last_name' => 'ahmadi',
'fname' => 'َabdullah',
'school_class_fa_name' => 'first class',
'year' => '1399',
'at_s_id' => '1',
'attendance_status_id' => '1',
),
.
.
.
10 =>
array (
'sda_id' => '26',
'st_id' => '858',
'student_id' => 'S99888',
'name' => 'ٌsafa',
'last_name' => 'ahmadi',
'fname' => 'َabdullah',
'school_class_fa_name' => 'first class',
'year' => '1399',
'at_s_id' => '1',
'attendance_status_id' => '2',
),
.
.
.
)
如您所见,这两个数组中的所有数据都相同,除了 attendance_status_id
。
所以我想知道如何在如下视图中查询并在一行中显示它们:
|st_id|student_id|name|last_name|fname|school_class_fa_name|year|at_s_id|attendance_status_id(that value is 1)|attendance_status_id(value is tow)|
|-----|----------|----|---------|-----|--------------------|----|-------|--------------------------------------|----------------------------------|
|858|S99888|safa|ahmadi|abdullah|first class|1399|1|1|2|
您可以使用下面的查询来解决您的问题。
return $this->db->select('sda.id sda_id,st_id,student.student_id,student.name,last_name,fname,school_class_fa_name,
year,
SUM(CASE attendance_status_id
WHEN "1" THEN 1
ELSE 0
END) AS present
, SUM(CASE attendance_status_id
WHEN "2" THEN 1
ELSE 0
END) AS absent
, SUM(CASE attendance_status_id
WHEN "3" THEN 1
ELSE 0
END) AS sick
, SUM(CASE attendance_status_id
WHEN "4" THEN 1
ELSE 0
END) AS st_leave
,attendance_status_id,date,description')
->from('student_daily_attendance sda')
->join('student', 'student.st_id=sda.student_id')
->join('school_class', 'school_class.school_class_id=sda.school_class_id')
->join('jalali_months', 'jalali_months.id=sda.month_id')
->where('sda.year', $data['edu_year'])
->where('sda.school_class_id', $data['grades'])
->where('sda.class_id', $data['class'])
->order_by('sda.id','desc')
->group_by('sda.student_id')
->get()->result_array();
这是我的 table,我想从这个 table 中计算 attendance_status_id
,其中年份 1399
和 attendance_status_id
值相同。例如,在这个 table 中,两个 attendance_status_id
具有相同的值 1
,所以当我将它算作 present_status
而另一个 2
算作 absent_status
我该怎么做?
这是我的功能:
public function get_student_attendance_daily_report($data)
{
return $this->db->select('sda.id sda_id,st_id,student.student_id,student.name,last_name,fname,school_class_fa_name,
year,COUNT(attendance_status_id) as at_s_id,attendance_status_id,date,description')
->from('student_daily_attendance sda')
->join('student', 'student.st_id=sda.student_id')
->join('school_class', 'school_class.school_class_id=sda.school_class_id')
->join('jalali_months', 'jalali_months.id=sda.month_id')
->where('sda.year', $data['edu_year'])
->where('sda.school_class_id', $data['grades'])
->where('sda.class_id', $data['class'])
->order_by('sda.id','desc')
->group_by('sda.student_id,sda.attendance_status_id')
->get()->result_array();
}
结果如下:
array (
'Total' => 36,
)array (
0 =>
array (
'sda_id' => '85',
'st_id' => '858',
'student_id' => 'S99888',
'name' => 'ٌsafa',
'last_name' => 'ahmadi',
'fname' => 'َabdullah',
'school_class_fa_name' => 'first class',
'year' => '1399',
'at_s_id' => '1',
'attendance_status_id' => '1',
),
.
.
.
10 =>
array (
'sda_id' => '26',
'st_id' => '858',
'student_id' => 'S99888',
'name' => 'ٌsafa',
'last_name' => 'ahmadi',
'fname' => 'َabdullah',
'school_class_fa_name' => 'first class',
'year' => '1399',
'at_s_id' => '1',
'attendance_status_id' => '2',
),
.
.
.
)
如您所见,这两个数组中的所有数据都相同,除了 attendance_status_id
。
所以我想知道如何在如下视图中查询并在一行中显示它们:
|st_id|student_id|name|last_name|fname|school_class_fa_name|year|at_s_id|attendance_status_id(that value is 1)|attendance_status_id(value is tow)|
|-----|----------|----|---------|-----|--------------------|----|-------|--------------------------------------|----------------------------------|
|858|S99888|safa|ahmadi|abdullah|first class|1399|1|1|2|
您可以使用下面的查询来解决您的问题。
return $this->db->select('sda.id sda_id,st_id,student.student_id,student.name,last_name,fname,school_class_fa_name,
year,
SUM(CASE attendance_status_id
WHEN "1" THEN 1
ELSE 0
END) AS present
, SUM(CASE attendance_status_id
WHEN "2" THEN 1
ELSE 0
END) AS absent
, SUM(CASE attendance_status_id
WHEN "3" THEN 1
ELSE 0
END) AS sick
, SUM(CASE attendance_status_id
WHEN "4" THEN 1
ELSE 0
END) AS st_leave
,attendance_status_id,date,description')
->from('student_daily_attendance sda')
->join('student', 'student.st_id=sda.student_id')
->join('school_class', 'school_class.school_class_id=sda.school_class_id')
->join('jalali_months', 'jalali_months.id=sda.month_id')
->where('sda.year', $data['edu_year'])
->where('sda.school_class_id', $data['grades'])
->where('sda.class_id', $data['class'])
->order_by('sda.id','desc')
->group_by('sda.student_id')
->get()->result_array();