总结关于thinkphp中where查询条件的设置

多个条件数组查询写法:

$User = new UserModel();

$map['id'] = array('neq',1);

$map['name'] = 'yuanye';

$User->where($map)->select();

查询“或”条件(_logic)

$where['name'] = array('like', '%yuauye%');

$where['title'] = array('like','%yuauye%');

$where['_logic'] = 'or';

$User->where($where)->select();

结果为:( name like '%yuauye%') OR ( title like '%yuauye%')

如果需要使用字符串模式查询使用方法

$User = new UserModel();

$map['_string'] = 'status=1 AND score>10';

$User->where($map)->select();

字符串模式查询(_string)查询多个“与”条件中嵌套“与”条件使用,例如:

$User = M("User"); // 实例化User对象

$map['id'] = array('neq',1);

$map['name'] = 'ok';

$map['_string'] = 'status=1 AND score>10';

$User->where($map)->select();

结果为:( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 )

where between查询,例如查询某一个时间段内的数据,用法如下:

$where['create_time']=array('between',[$startTime,$endDate]);

使用数组格式的时候  between后面需要使用数组[$startTime,$endDate]

where in查询,需要区分两种情况,

in后面接的是数值数组

使用的是数值数组:array('in','1,5,8');

in 字符数组,使用字符数组的时候 ,后面的数组查询条件需要拼接

  $vinarr1 = '';
   for ($i = 0; $i < count($vinarr); $i ++) {
                        $vinarr1 = $vinarr1 . "'" . $vinarr[$i] . "',";
   }
 $sqlBinds = "fvin in(" . $vinarr1 . "'')";
  $sqlm = 'select MAX(accumula_time) as MaxaccumulaTime, MIN(accumula_time) as MinaccumulaTime,fvin , ltime as time from forklift_location WHERE ' . $sqlBinds . ' and ltime >= ? and ltime < ? group by  fvin';
                    $sqlBinds = array(
                        $preDays,
                        $nowDate
                    );
                    $resultm = $this->forkliftLocation->query($sqlm, $sqlBinds);

代码的使用出处来自查询单辆叉车一个月内的平均工作时长,getmonthaverage

原生的sql

修改查询的字段,将查询的字段拼接处理后再查询出,例如使用as当成新需要的字段

select 姓名, 工资, 面积, 金额, (工资+金额/1000) as 实发工资 from 职工,仓库, 订购单   where 职工.职工号=订购单.职工号 and 职工.仓库号=仓库.仓库号   

 

全连接查询

    除满足连接条件的记录显示外,两张表中的不能满足条件的记录也显示在查询结果集中

    

[sql] view plain copy

select 姓名,城市 from 仓库 full join 职工 on 职工.仓库号=仓库.仓库号 and 城市 is not null and  

姓名 like '%王%';  

主要将两个或者更多个查询的结果组合为单个结果集,该结果集包含联合查询中的全部查询的全部行

        

[sql] view plain copy

select 仓库号 from 仓库 where 城市='北京'  

union  

select 仓库号 from 职工 where 工资>2000  

 

[sql] view plain copy

select 仓库号 from 仓库 where 城市='北京'  

union  

select 仓库号 from 职工 where 工资>2000  

  

select distinct 仓库.仓库号 from 仓库, 职工 where 仓库.仓库号=职工.仓库号 and (城市='北京' or 工资>2000)  

 

使用union all 保留重复行

[sql] view plain copy

select 仓库号 from 仓库 where 城市='北京'  

union all  

select 仓库号 from 职工 where 工资>2000  


交运算(intersect)

  可以将两个select语句的查询结果通过交运算合并成一个查询结果

   

[sql] view plain copy

select 仓库号 from 仓库 where 城市='北京'  

intersect  

select 仓库号 from 职工 where 工资>2000  

 

[sql] view plain copy

  1. select distinct 仓库.仓库号 from 仓库, 职工 where 城市='北京' and 仓库.仓库号=职工.仓库号 and 工资>2000  

 

 

 

差运算(except)

     可以计算两个select查询结果之间的数据差,即返回在一个查询结果中存在,但在另一个查询结果中不存在的所有行。

    

[sql] view plain copy

select 仓库号 from 仓库 where 城市='北京'   

except   

select 仓库号 from 职工 where 工资>2900  

 

[sql] view plain copy

select 仓库号 from 仓库 where 城市='北京' and 仓库号 not in(select 仓库号 from 职工 where 工资>2900)  


 


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章