我想在我的数据库中计算特定字段取决于ID_number

时间:2019-03-28 07:36:32

标签: php codeigniter

这是我的模特

public function total_late()
    {

        $query = "SELECT sum(late_deduction) as late_deduction FROM tbl_dtr";
        $result = $this->db->query($query);
        return $result->row()->late_deduction;
    }

这是数据库的图像:

db

这是我的桌子的图像:

table

这是我视图中表格的另一个模型

function get_faculty_payroll($limit, $start, $string = "", $orderField, $orderDirection)
    {  
       $this->db->group_start()
                        ->where('tbl_employee' . '.department_id', 3)
                ->group_end()
                ->group_start()
                        ->or_like('first_name', $string)
                        ->or_like('last_name', $string)
                ->group_end()
                ->join('tbl_employee_department', 'tbl_employee.department_id = tbl_employee_department.department_id')
                ->join('tbl_rates', 'tbl_employee.ID_number = tbl_rates.ID_number')
                ->limit($limit, $start)
                ->order_by($orderField, $orderDirection);
       $query = $this->db->get('tbl_employee');
       return $query->result();
    }

这是我的桌子:

enter image description here

2 个答案:

答案 0 :(得分:1)

您的控制器

$stmt = $this->db->prepare("
    SELECT sum(late_deduction) as late_deduction
    FROM tbl_dtr WHERE ID_number = ?
");

$stmt->execute(array($ID_number));

您的模型

axios.post("/api/get_data/"

但是,此代码更好,因为它可以安全地处理参数并避免SQL注入漏洞:

http://localhost:8080/...

答案 1 :(得分:0)

尝试一下

public function total_late()
{
  $query = "SELECT sum(late_deduction) as late_deduction FROM tbl_dtr where ID_number = 'C-08011'";
  $result = $this->db->query($query);
  return $result->row()->late_deduction;
}