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. hdu1512 Monkey King

    Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...

  2. 蓝桥杯-扑克牌移动-java

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

  3. linux 内核的rt_mutex (realtime互斥体)

    linux 内核有实时互斥体(锁),名为rt_mutex即realtime mutex.说到realtime一定离不开priority(优先级).所谓实时,就是根据优先级的不同对任务作出不同速度的响应 ...

  4. luogu P1015 回文数

    题目描述: 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制 ...

  5. ionic之$ionicHistory

    $ionicHistory 定义:当用户通过导航栏切换视图页面的时候,ionicHistory起到跟踪视图的作用,类似的浏览器的行为方式,一个ionic应用程序能够保持以前的视图,当前视图,和前视图( ...

  6. 调停者(Mediator)模式

    调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从而使它们可以较松散地耦合.当这些对象中的某些对象之间的相互作用发生改变时,不会立即影响到其他的一些 ...

  7. 刑天DDOS攻击器下一版本即将使用NTP放大功能

    刑天DDOS攻击器下一版本即将使用NTP放大功能       在一次无语实验中无意发现NTP方法后的攻击流量相当可观,Linux实测G口高达30G,也就是说最大可以放大30倍的攻击流量是何等的威武.而 ...

  8. Vue中comoputed中的数据绑定

    Vue中的数据实现响应式绑定是在初始化的时候利用definePrototype的定义set和get过滤器,在进行组件模板编译时实现water的监听搜集依赖项,当数据发生变化时在set中通过调用dep. ...

  9. 【JAVAWEB学习笔记】27_Redis:在Linux上的安装、Jedis和常用命令

    一.Redis简介 1.关于关系型数据库和nosql数据库 关系型数据库是基于关系表的数据库,最终会将数据持久化到磁盘上,而nosql数据     库是基于特殊的结构,并将数据存储到内存的数据库.从性 ...

  10. 【CSS Cookbook】笔记摘要(二)

     页面元素 使用text-align性质可以居中显示块级元素中的文字.把margin-left和margin-right设为auto时,该元素则会相对于父元素居中显示.但是现在流行的一些较低版本的浏览 ...