【问题标题】:I want to convert a normal mysql query to codeigniter我想将普通的 mysql 查询转换为 codeigniter
【发布时间】:2016-11-25 03:54:25
【问题描述】:

我想将普通的 mysql 选择查询转换为 codeigniter

SELECT count(*) leave_status, teacher_id FROM teacher_attendance WHERE teacher_id='1' & leave_status='1' group by teacher_id

这是我尝试过的

$attendance = $this->db->get_where('teacher_attendance', array('year' => $running_year, 'timestamp' => $timestamp, 'teacher_id' => $row['teacher_id']))->result_array();

我想要做的是通过教师 ID 获取 leave_status 的计数

【问题讨论】:

  • 如果使用count(*),您将得到一行。那为什么teacher_id
  • 我想过滤特定教师的 leave_status,所以我想使用教师 ID?对

标签: mysql codeigniter


【解决方案1】:
$this->db->select("count(*) as leave_status, teacher_id");
    $this->db->from("teacher_attendance");
    $this->db->where("teacher_id = 1 AND leave_status='1'");
    $this->db->group_by("teacher_id");
    $query = $this->db->get();       
    if ($query->num_rows() > 0) {
        $result = $query->result_array();
    }

【讨论】:

  • 当我给出 print_r() 并检查 $result = $query->result_array(); print_r($结果);它打印一个数组 ( [0] => Array ( [leave_status] => 1 [teacher_id] => 1 ) ) 所以如何只打印 [leave_status] => 1,谢谢
  • 如果你想要行,你可以使用 $query->row()。
  • 当我使用 $query->row() 它返回 stdClass 对象 ( [leave_status] => 1 ) 所以现在我想给这个 leave_status 一个回显并打印它
  • $this->db->where("teacher_id",1)$this->db->where("leave_status", 1);
猜你喜欢
  • 2017-05-29
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
  • 2021-12-29
  • 2023-03-14
  • 2020-01-18
  • 2015-06-08
  • 1970-01-01
相关资源
最近更新 更多