<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class QueryController extends Controller{
//快捷查询 - 是一种多字段查询的简化写法,
//多个字段之间用‘|’隔开表示OR
//用‘&’隔开表示AND
public function Query1(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
//不同字段 的 相同查询
$usr=M(‘User‘);
//$map[‘user|email‘]=‘樱桃小丸子‘;
$map[‘user&email‘]=‘樱桃小丸子‘;
echo "<pre>";
var_dump($usr->where($map)->select());
}
public function Query2(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
//不同字段 的 不同查询
$usr=M(‘User‘);
$map[‘id&user‘]=array(1,‘柯南‘,‘_multi‘=>TRUE);
//设置‘_multi‘为true,是为了让id对应1,让user对应“柯南”,否则
//会出现id对应了1,还要对应“柯南”的情况,而且,这设置要放在数组最后
echo "<pre>";
var_dump($usr->where($map)->select());
}
public function Query3(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
// 结合表达式的 快捷查询
$usr=M(‘User‘);
//$map[‘id&user‘]=array(1,‘柯南‘,‘_multi‘=>TRUE);
//设置‘_multi‘为true,是为了让id对应1,让user对应“柯南”,否则
//会出现id对应了1,还要对应“柯南”的情况,而且,这设置要放在数组最后
$map[‘id&user‘]=array(array(‘gt‘,1),‘樱桃小丸子‘,‘_multi‘=>true);
echo "<pre>";
var_dump($usr->where($map)->select());
}
public function Query4(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
//区间查询
//比如 id>1 and id<3
//或者 id>1 or id<3
$user=M("User");
$map[‘id‘]=array(array(‘gt‘,1),array(‘lt‘,3));//注意没有等于
echo "<pre>";
var_dump($user->where($map)->select());
$map[‘id‘]=array(array(‘lt‘,1),array(‘gt‘,3),‘or‘);
echo "<pre>";
var_dump($user->where($map)->select());
}
//组合查询 - 基于索引数组查询方式的扩展性查询,
// 可以添加字符串查询(_string)、复合查询(_complex)、请求字符串查询(_query)
//由于采用的是索引数组,重复的会被覆盖
public function Query5(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
$user=M(‘User‘);
$map[‘id‘]=array(‘eq‘,1);
$map[‘_string‘]=‘user="柯南" AND email="[email protected]"‘;
// $map[‘_logic‘]=‘OR‘;
echo "<pre>";
var_dump($user->where($map)->select());
}
public function Query6(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
$user=M(‘User‘);
$map[‘id‘]=array(‘gt‘,1);
//请求字符串 查询 - 这是URL方式,不需要加引号
$map[‘_query‘]=‘user=樱桃小丸子&[email protected]&_logic=OR‘;
//推荐使用,因为字段名有``护起来
echo "<pre>";
var_dump($user->where($map)->select());
}
public function Query7(){
echo ‘<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>‘;
$user=M(‘User‘);
$where[‘user‘]=array(‘like‘,‘%小%‘);
$where[‘id‘]=1;
$where[‘_logic‘]=‘OR‘;
$map[‘_complex‘]=$where;
$map[‘id‘]=3;
$map[‘_logic‘]=‘OR‘;
echo "<pre>";
var_dump($user->where($map)->select());
}
//统计 查询
public function TjQuery(){
$user=M(‘User‘);
var_dump($user->count());//统计记录总数
var_dump($user->count(‘email‘));//统计字段email总数,遇到null跳过
echo "<br />";
var_dump($user->max(‘id‘));
var_dump($user->min(‘id‘));
var_dump($user->avg(‘id‘));
var_dump($user->sum(‘id‘));
}
// 动态查询
// 借助 PHP5 语言特性,ThinkPHP实现动态查询
public function Query_getby(){
//getBy动态查询 //注意找到多个时,只返回一个
$user=M(‘User‘);
var_dump($user->getByEmail(‘[email protected]‘));
var_dump($user->getByUser(‘旗木卡卡西‘));
var_dump($user->getByUser(array(‘LIKE‘,‘%小%‘)));
}
//getFieldBy查询
public function Query_getfieldby(){
$user=M(‘User‘);
//var_dump($user->getFieldByUser(‘旗木卡卡西‘,‘id‘));
var_dump($user->getFieldByUser(‘旗木卡卡西‘,array(‘id‘,‘email‘)));
//注意,即使想这样想返回多个字段的结果,也会只返回一个(id的 值)
}
//SQL原生查询
public function Query_read(){
//ThinkPHP支持原生SQL query读取
$user=M(‘User‘);
echo "<pre>";
var_dump($user->query(‘SELECT * FROM think_user‘));
}
//execute写入
public function Execute_write(){
$user=M(‘User‘);
var_dump($user->execute(‘UPDATE think_user SET user="蜡笔大新" WHERE id=1‘));
}
}
?>