如何将sql查询转换为codeigniter查询

cpjpxq1n  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(453)

有人能帮我转换这个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查询,如下图所示

kjthegm6

kjthegm61#

你能做到的

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() 如果一个输出 array . result() 是多个 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();
}
7hiiyaii

7hiiyaii2#

当查询语句没有需要有条件更改的子句时,则使用 $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 = $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);

相关问题