CI 数据库使用积累

一、      or_like使用

情景:WMS库存列表过滤器通过产品名称或者SKU查询。

通常此情况采用CI框架提供的or_like语句,如

$this->db->like(Product_model::TABLENAME . '.' .'product_name', value);

$this->db->or_like(Product_model::TABLENAME.'.'.'origin_sku', value);

但此情况如果在有用$this->db->where的时候会出现问题。如新增

$this->db->where(Product_model::TABLENAME . '.' .'cate_id', $value);

实际呈现出来的sql语句是类似

Where 'product_name' like %value% or 'origin_sku' like %value% and 'cate_id' = value

但我们实际想要的结果是如下

Where ('product_name' like %value% or 'origin_sku' like %value%) and 'cate_id' = value

二者之间区别就是少了一对括号,所查询的结果集则不一样。

如果查询的结果集是通过上述包含括号的SQL查询语句得到的,则可以通过修改CI框架。查找到DB_active_rec.php文件中的_compile_select函数实现,修改如下:

// Write the "LIKE" portion of the query

if (count($this->ar_like) > 0)

{

if (count($this->ar_where) > 0)

{

$sql .= "\nAND (";

}

$sql .= implode("\n", $this->ar_like);

if (count($this->ar_where) > 0)

{

$sql .= ")";

}

}

但修改此框架会导致原有功能缺失。有的用户希望得到查询的结果集就刚刚好是没有包含括号的SQL语句,则又得回滚代码。

故我们另外采用的一种方式是通过CI 框架的where语句结合sql查询语句方式。如下:

$left = "'%";

$right = "%'";

$sql = '(' . Product_model::TABLENAME . '.'  . 'product_name like ' . $left . $value . $right . ' or ' . Product_model::TABLENAME . '.'  . 'origin_sku like ' . $left . $value . $right . ')';

$this->db->where($sql, null, false);

Where语句采用CI框架提供的第四种方式:自定义字符串方式,但注意的是where语句传参在CI框架用户指南上是有误的,使用方式应是$this->db->where($sql, null, false)。

二、      distinct使用

情景:通过过滤器查询产品名称来获取售后申请信息。售后申请信息是单独的数据表tbl_sales_order,而相应的售后申请信息所包含的产品信息则是在tbl_sales_order_products,但tbl_sales_order_product则只包含产品ID,也就是product_id,具体的产品名称需通过product_id查询tbl_products来获取。如果要通过查询产品名称来获取售后申请则需要联合售后申请信息表、售后产品表以及产品名称表。

通常采用的语句如下:

$this->db->from(self::TABLENAME);

$this->db->join(Sales_order_products_model::TABLENAME,self::TABLENAME . '.' . 'id' . '=' . Sales_order_products_model::TABLENAME . '.' . 'sale_order_id');

$this->db->join(Product_model::TABLENAME,Sales_order_products_model::TABLENAME . '.' . 'product_id' . '=' . Product_model::TABLENAME . '.' . 'id');

if(!is_null($per_page)){

$this->db->limit($per_page, $page*$per_page);

}

$this->db->select(self::TABLENAME . '.' . '*');

上述情况在一张售后申请表对应一条产品信息的时候是可行的,但如果有一张售后申请有多条产品信息的时候,会出现多条冗余的售后申请信息,故需去掉冗余的信息,可采用CI框架提供的函数distinct。

$this->db->distinct();

三、      union使用

情景:WMS系统中要呈现一张退货单报表,包含售后退货以及拒收/未妥投订单信息。其中包含售后退货和拒收/未妥投两种业务,二者之中只存在部分字段可复用,此报表只提供通过起始时间和截止时间查询结果。这意味着在输出结果集是需要合并表单数据,可通过union字段。

正常的一种使用方式是获取两个结果集进行合并输出。如下例子:

// Query #1
 
$this->db->select('title, content, date');
$this->db->from('mytable1');
$query1 = $this->db->get()->result();
 
// Query #2
 
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query2 = $this->db->get()->result();
 
// Merge both query results
 
$query = array_merge($query1, $query2);

但此方式在进行分页显示需要自行对数组进行处理。

第二种方式是采用网上提供的一种方式,调用_compile_select和_reset_select接口。如下:

$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
$subQuery1 = $this->db->_compile_select();
 
$this->db->_reset_select();
 
// #2 SubQueries no.2 -------------------------------------------
 
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();
$subQuery2 = $this->db->_compile_select();
 
$this->db->_reset_select();
 
// #3 Union with Simple Manual Queries --------------------------
 
$this->db->query("select * from ($subQuery1 UNION $subQuery2) as unionTable");
 
// #3 (alternative) Union with another Active Record ------------
 
$this->db->from("($subQuery1 UNION $subQuery2)");
$this->db->get();

但此方式实际使用是会报错,_compile_select和_reset_select接口是保护性的,无法调用。

第三种方式则是采用CI框架提供的last_query接口,获取上述查询语句(注:不是查询结果),然后再通过query接口合并SQL语句进行查询,实际WMS采用的就是此方式,实际用例如下:

public function get_return_by_condition($condition=array(), $page,$per_page='10'){

$this->db->select(array('tbl_sales_order.id as id', 'order_id', 'create_date'));

$this->db->from('tbl_sales_order');

$this->db->join('tbl_sales_operate_log', 'tbl_sales_order.id = tbl_sales_operate_log.sale_order_id');

$this->db->where('tbl_sales_order.product_status', 7);

$this->db->where('tbl_sales_order.payment_status', 135);

$this->db->where('tbl_sales_operate_log.product_status', 7);

$this->db->where('tbl_sales_operate_log.payment_status', 135);

$this->db->distinct();

//return $query2 = $this->db->get()->result();

$query = $this->db->get();

$subQuery1 = $this->db->last_query();

$this->db->from('tbl_purchase_order');

$this->db->join('tbl_order_stockout', 'tbl_purchase_order.id = tbl_order_stockout.order_purchase_id');

$this->db->join('tbl_order_action', 'tbl_order_action.order_id = tbl_order_stockout.order_id');

$this->db->where('tbl_order_action.mark', 1); //1表示订单状态

$this->db->where('tbl_order_action.action_status', '123');  //订单取消为123

$this->db->distinct();

$this->db->select(array('tbl_purchase_order.id as id',

'tbl_purchase_order.create_date as create_date',

'tbl_order_stockout.order_id as order_id',

));

//return $this->db->get()->result();

$query = $this->db->get();

$subQuery2 = $this->db->last_query();

$sql = 'select * from ($subQuery1 UNION $subQuery2) as unionTable';

if(!is_null($per_page)){

$sql .= 'limit ' . $per_page * $page . ', ' . $per_page;

}

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

}

第四种方式采用重新CI框架,支持UNION方式,具体可参考

http://stackoverflow.com/questions/2040655/union-query-with-codeigniters-active-record-pattern/8473729

CI 数据库使用积累的更多相关文章

  1. CI数据库操作_查询构造器类

    =================数据库操作======================1.数据库配置: config/database.php 用户名 密码 数据库 2 加载数据库类:$this-& ...

  2. CI 数据库操作总结

    最简单示例 $query = $this->db->query("YOUR QUERY"); foreach ($query->result() as $row) ...

  3. informix数据库知识积累

    一.嵌套查询 informix子查询:嵌套查询(1)select first 20 * from (select first 40 * from hlrquery_log order by id de ...

  4. oracle数据库语句积累

    1.从一个表选出数据更新另一个表(后面的exists一定要加) update jqhdzt set shid = (select shid from v_plat_userjqinfo t where ...

  5. INFORMIX数据库常用命令

    INFORMIX数据库常用命令 一.onstat命令集 1.onstat  - 说明:查看数据库当前的状态 用法:onstat  - 2.onstat  -c 说明:查看数据库的配置文件 用法:ons ...

  6. 数据库子查询和join的比较

    子查询进行SELECT语句嵌套查询,可以一次完成很多逻辑上需要多个步骤才能完成的SQL操作.子查询虽然很灵活,但是执行效率并不高. select goods_id,goods_name from go ...

  7. CentOS6.5配置PHP CI程序

    步骤: 1.安装CentOS6.5系统:     1.选择PHP+Mysql环境 2.关闭防火墙和SeLinux     1.chkconfig --level 35 iptables off     ...

  8. CodeIgniter学习笔记一:基本结构、控制器、视图、超级对象、数据库

    一.基本结构 CodeIgniter3.0.0解压后有8个文件,分别是: application:项目文件 system:系统(框架)文件,为方便升级,不建议修改 user_guid:用户手册,不需要 ...

  9. 积累——SQLCommand命令

    SQLcommand表示要对SQL数据库运行的一个 T-SQL 语句或存储过程.以便运行大量操作或处理数据库结构. 在对数据库訪问的时候,就经经常使用到这个.看看它是怎么做到的吧! 一.属性 Comm ...

随机推荐

  1. C专家编程阅读笔记

    周末闲来无事,(哗),好久之前买的C专家编程一直没看,翻起来看了一下 尽量不使用unsigned 尽量不要在代码中使用unsigned,尤其是一些看起来是无符号类型的数字,比如年龄等,因为难免要使用u ...

  2. 蓝桥杯-打印大X-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  3. 在myeclipse下安装svn插件,出现了Could not generate DH keypair,这么一个错误。

    解决方法: window-->preferences-->Team-->SVN-->SVN接口 选择:JavaHL 去https://sliksvn.com/download/ ...

  4. C++接口的定义与实现的详细过程

    1.接口的定义与实现 所谓的接口,即将内部实现细节封装起来,外部用户用过预留的接口可以使用接口的功能而不需要知晓内部具体细节.C++中,通过类实现面向对象的编程,而在基类中只给出纯虚函数的声明,然后在 ...

  5. C#基础知识-函数的定义和调用(五)

    函数也可以称为方法,可以很方便的把一些行为封装到函数里面,当调用这一函数时会把函数块里面的代码按照顺序执行,方法可以有多种形式,有无参数,有无返回值等. 1. 函数的定义   函数定义的基本格式: s ...

  6. js原型链部分详细使用说明案例

    1. 'index.html'文件 ```html <!DOCTYPE html> <html lang="en"> <head> <me ...

  7. LINQ基础(二)

    本文主要介绍LINQ查询操作符 LINQ查询为最常用的操作符定义了一个声明语法.还有许多查询操作符可用于Enumerable类. 下面的例子需要用到LINQ基础(一)(http://www.cnblo ...

  8. javascript设计模式详解之策略模式

    接上篇命令模式来继续看下js设计模式中另一种常用的模式,策略模式.策略模式也是js开发中常用的一种实例,不要被这么略显深邃的名字给迷惑了.接下来我们慢慢看一下. 一.基本概念与使用场景: 基本概念:定 ...

  9. BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]

    错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...

  10. 使用DBCP连接池对连接进行管理

    //需要引用的jar包有4个,分别是commons-pool2-2.4.2.jar.commons-dbcp2-2.1.1.jar.mysql-connector-java-5.1.42-bin.ja ...