首页 > 代码库 > thinkphp-查询语句
thinkphp-查询语句
//SQL查询语句
//数组索引查询
/*
$user=M(‘Sks‘);
$condition[‘id‘]=1;
$condition[‘name‘]=160;
$condition[‘pass‘]=asd;
echo "<pre>";
var_dump($user->where($condition)->select());
echo "</pre>";
*/
//对象查询
/*
$user=M(‘Sks‘);
$condtion=new \stdClass();
$condition->id=1;
$condition->name=160;
$condition->pass=asd;
echo "<pre>";
var_dump($user->where($condition)->select());
echo "</pre>";
*/
//表达式查询
//$user=M(‘Sks‘);
//$map[‘id‘]=array(‘eq‘,1); //查询id等于1的
//$map[‘id‘]=array(‘neq‘,1); // 不等于
//$map[‘id‘]=array(‘gt‘,1); // 大于
//$map[‘id‘]=array(‘egt‘,1); // 大于等于
//$map[‘id‘]=array(‘lt‘,1); // 小于
//$map[‘id‘]=array(‘elt‘,1); // 小于等于
//$map[‘id‘]=array(‘like‘,‘%小%‘); //模糊查询-有‘小‘的
//$map[‘id‘]=array(‘notlike‘,‘%小%‘); //模糊查询-没有‘小‘的
//$map[‘id‘]=array(‘like‘,array(‘%小%‘,‘%大%‘),‘AND‘); //模糊查询-即有‘小’的,又有‘大’的
//$map[‘id‘]=array(‘between‘,‘1,3‘);//1,3区间查询
//$map[‘id‘]=array(‘notbetween‘,‘1,3‘);//(不在)1,3区间查询
//$map[‘id‘]=array(‘in‘,‘1,2,3‘); //in查询1,2,3
//$map[‘id‘]=array(‘notin‘,1,2,3); //(不在)in查询1,2,3
//$map[‘id‘]=array(‘exp‘,‘=1‘); //表达式查询,支持sql语法
//查询id等于1的 >1 !=1 ......
/*
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
//不同字段相同查询条件
/*
$user=M(‘Sks‘);
$map[‘name|pass‘]=‘160‘;
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
//同字段不同查询条件
/*
$user=M(‘Sks‘);
$map[‘id|pass‘]=array(1,123,‘_multi‘=>true);
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
/*
$user=M(‘Sks‘);
$map[‘id‘]=array(array(‘gt‘,1),array(‘lt‘,4));
$map[‘id‘]=array(array(‘gt‘,1),array(‘lt‘,4),‘OR‘);
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
//字符串查询(_string)
/*
$user=M(‘Sks‘);
$map[‘id‘]=array(‘eq‘,1);
$map[‘_string‘]=‘name="160" AND pass="asd"‘;
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
//请求字符串查询(_query)
/*
$user=M(‘Sks‘);
$map[‘id‘]=array(‘eq‘,1);
$map[‘_query‘]=‘name=160&pass=asd‘;
$map[‘_logic‘]=‘OR‘;
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
//复合查询
/*
$user=M(‘Sks‘);
$map[‘id‘]=array(‘eq‘,1);
$where[‘id‘]=2;
$map[‘_complex‘]=$where;
$map[‘_logic‘]=‘OR‘; //OR:或 AND:和
echo "<pre>";
var_dump($user->where($map)->select());
echo "</pre>";
*/
//数据总条数
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->count());
echo "</pre>";
*/
//字段总条数,遇null不统计
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->count(‘id‘));
echo "</pre>";
*/
//最大值
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->max(‘id‘));
echo "</pre>";
*/
//最小值
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->min(‘id‘));
echo "</pre>";
*/
//平均值
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->avg(‘id‘));
echo "</pre>";
*/
//求总和
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->sum(‘id‘));
echo "</pre>";
*/
//getBy动态查询
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->getByname(‘160‘));
echo "</pre>";
*/
//getFieldBy动态查询
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->getFieldBySks(‘123‘,‘pass‘));
echo "</pre>";
*/
//SQL读取
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->query(‘SELECT * FROM think_sks‘));
echo "</pre>";
*/
//esecute写入 或修改
/*
$user=M(‘Sks‘);
echo "<pre>";
var_dump($user->execute(‘UPDATE think_sks SET name="460" WHERE id=2‘));
echo "</pre>";
*/
thinkphp-查询语句