如何将SQL查询转换为Codeigniter查询

时间:2018-11-21 16:45:39

标签: mysql codeigniter codeigniter-3

有人可以帮我转换此 Sql查询

SELECT *
FROM customer c
LEFT JOIN customer_order co 
ON c.customer_number = co.customer_number 
AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid'
AND c.order_status = 'unserve'
AND co.cus_ord_no IS null

进入 Codeigniter查询,例如下面的图片

enter image description here

2 个答案:

答案 0 :(得分:2)

当查询语句中没有需要有条件更改的子句时,可以使用$this->db-query()

$sql = "SELECT * FROM customer c LEFT JOIN customer_order co 
ON c.customer_number=co.customer_number AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid' AND c.order_status='unserve' AND co.cus_ord_no IS null";

$query = $this->db->query($sql)->result();
echo json_encode($query);

明智的做法是对query()的返回值进行检查,因为如果失败(返回false),则对result()的调用将引发异常。可以处理的一种方法就是这样。

$query = $this->db->query($sql);
if($query !== FALSE) 
{
    echo json_encode($query->result());
    return;
}

echo json_encode([]); // respond with an empty array

查询生成器(QB)是一个很好的工具,但是它通常会显得过分杀伤力。创建一个字面传递给$db->query()的字符串会增加很多开销。如果您知道该字符串,并且由于某种原因不需要对其进行重组,则不需要QB。

当您希望有条件地更改查询语句时,

QB最有用。排序可能是一种可能的情况。

if($order === 'desc'){
   $this->db->order_by('somefield','DESC'); 
} else {
       $this->db->order_by('somefield','ASC'); 
}

$results = $this->db
    ->where('other_field', "Foo")
    ->get('some_table')
    ->result();

因此,如果$order的值为'desc',则查询语句将为

SELECT * FROM some_table WHERE other_field = 'Foo' ORDER BY somefield 'DESC' 

但是,如果您坚持使用Query Builder,我相信这是您的答案

$query = $this->db
    ->join('customer_order co', "c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared')", 'left')
    ->where('c.customer_status','unpaid')
    ->where('c.order_status','unserve')
    ->where('co.cus_ord_no IS NULL')
    ->get('customer c');

//another variation on how to check that the query worked
$result = $query ? $query->result() : [];
echo json_encode($result);

答案 1 :(得分:2)

你可以

public function view_customers()
{
    $sql = "SELECT * FROM customer c LEFT JOIN customer_order co ON c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared') WHERE c.customer_status='unpaid' AND c.order_status = 'unserve' AND co.cus_ord_no IS null";

    return $this->db->query($sql)->result();
}

您可以使用row()object的一个输出,或者如果是row_array()的一个输出,则使用arrayresult()是多个objects,而result_array()是多个arrays

我通常的做法是这样的:

控制器:

public function view()
{
    $this->load->model('My_Model');

   $data = new stdclass;
   $data->user_lists = $this->my_model->view_users(array('nationality'=>'AMERICAN'));
}

型号:

public function view_users($param = null) //no value passed
{
   $condition = '1';
   if (!empty($param)) { //Having this will trap if you input an array or not
     foreach ($param as $key=>$val) {
       $condition .= " AND {$key}='{$val}'"; //Use double quote so the data $key and $val will be read.
     }
   }

   $sql = "SELECT * FROM users WHERE {$condition}"; //Use double quote so the data $condition will be read.
   // Final out is this "SELECT * FROM users WHERE 1 AND nationality='AMERICAN'";

   return $this->db->query($sql)->result();
}