欢迎各位兄弟 发布技术文章
这里的技术是共享的
$User = M("User"); // 实例化User对象
$User->where('type=1 AND status=1')->select();
复制代码
SELECT * FROM think_user WHERE type=1 AND status=1
复制代码
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['status'] = 1;
// 把查询条件传入查询方法
$User->where($condition)->select();
复制代码
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
复制代码
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// 把查询条件传入查询方法
$User->where($condition)->select();
复制代码
SELECT * FROM think_user WHERE `name`='thinkphp' OR `account`='thinkphp'
复制代码
$User = M("User"); // 实例化User对象
// 定义查询条件
$condition = new stdClass();
$condition->name = 'thinkphp';
$condition->status= 1;
$User->where($condition)->select();
复制代码
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
复制代码
表达式 | 含义 |
---|---|
EQ | 等于(=) |
NEQ | 不等于(<>) |
GT | 大于(>) |
EGT | 大于等于(>=) |
LT | 小于(<) |
ELT | 小于等于(<=) |
LIKE | 模糊查询 |
[NOT] BETWEEN | (不在)区间查询 |
[NOT] IN | (不在)IN 查询 |
EXP | 表达式查询,支持SQL语法 |
$map['id'] = array('eq',100);
复制代码
$map['id'] = 100;
复制代码
$map['id'] = array('neq',100);
复制代码
$map['id'] = array('gt',100);
复制代码
$map['id'] = array('egt',100);
复制代码
$map['id'] = array('lt',100);
复制代码
$map['id'] = array('elt',100);
复制代码
$map['name'] = array('like','thinkphp%');
复制代码
'DB_LIKE_FIELDS'=>'title|content'
复制代码
$map['title'] = 'thinkphp';
复制代码
$map['a'] =array('like',array('%thinkphp%','%tp'),'OR');
$map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND');
复制代码
(a like '%thinkphp%' OR a like '%tp') AND (b not like '%thinkphp%' AND b not like '%tp')
复制代码
$map['id'] = array('between','1,8');
复制代码
$map['id'] = array('between',array('1','8'));
复制代码
$map['id'] = array('not in','1,5,8');
复制代码
$map['id'] = array('not in',array('1','5','8'));
复制代码
$map['id'] = array('in','1,3,8');
复制代码
$map['id'] = array('exp',' IN (1,3,8) ');
复制代码
$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值
$data['name'] = 'ThinkPHP';
$data['score'] = array('exp','score+1');// 用户的积分加1
$User->where('id=5')->save($data); // 根据条件保存修改的数据
复制代码
$User = M("User"); // 实例化User对象
$map['name|title'] = 'thinkphp';
// 把查询条件传入查询方法
$User->where($map)->select();
复制代码
name= 'thinkphp' OR title = 'thinkphp'
复制代码
$User = M("User"); // 实例化User对象
$map['status&title'] =array('1','thinkphp','_multi'=>true);
// 把查询条件传入查询方法
$User->where($map)->select();
复制代码
status= 1 AND title = 'thinkphp'
复制代码
$map['status&score&title'] =array('1',array('gt','0'),'thinkphp','_multi'=>true);
复制代码
status= 1 AND score >0 AND title = 'thinkphp'
复制代码
$map['id'] = array(array('gt',1),array('lt',10)) ;
复制代码
(`id` > 1) AND (`id` < 10)
复制代码
$map['id'] = array(array('gt',3),array('lt',10), 'or') ;
复制代码
$map['id'] = array(array('neq',6),array('gt',3),'and');
复制代码
$map['name'] = array(array('like','%a%'), array('like','%b%'), array('like','%c%'), 'ThinkPHP','or');
复制代码
(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'ThinkPHP')
复制代码
$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 )
复制代码
$map['id'] = array('gt','100');
$map['_query'] = 'status=1&score=100&_logic=or';
复制代码
`id`>100 AND (`status` = '1' OR `score` = '100')
复制代码
$where['name'] = array('like', '%thinkphp%');
$where['title'] = array('like','%thinkphp%');
$where['_logic'] = 'or';
$map['_complex'] = $where;
$map['id'] = array('gt',1);
复制代码
( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )
复制代码
$where['id'] = array('gt',1);
$where['_string'] = ' (name like "%thinkphp%") OR ( title like "%thinkphp") ';
复制代码
方法 | 说明 |
---|---|
Count | 统计数量,参数是要统计的字段名(可选) |
Max | 获取最大值,参数是要统计的字段名(必须) |
Min | 获取最小值,参数是要统计的字段名(必须) |
Avg | 获取平均值,参数是要统计的字段名(必须) |
Sum | 获取总分,参数是要统计的字段名(必须) |
$User = M("User"); // 实例化User对象
复制代码
$userCount = $User->count();
复制代码
$userCount = $User->count("id");
复制代码
$maxScore = $User->max('score');
复制代码
$minScore = $User->where('score>0')->min('score');
复制代码
$avgScore = $User->avg('score');
复制代码
$sumScore = $User->sum('score');
复制代码
query 执行SQL查询操作 | |
---|---|
用法 | query($sql,$parse=false) |
参数 | sql(必须):要查询的SQL语句 parse(可选):是否需要解析SQL |
返回值 | 如果数据非法或者查询错误则返回false 否则返回查询结果数据集(同select方法) |
$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->query("select * from think_user where status=1");
复制代码
execute用于更新和写入数据的sql操作 | |
---|---|
用法 | execute($sql,$parse=false) |
参数 | sql(必须):要执行的SQL语句 parse(可选):是否需要解析SQL |
返回值 | 如果数据非法或者查询错误则返回false 否则返回影响的记录数 |
$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->execute("update think_user set name='thinkPHP' where status=1");
复制代码
方法名 | 说明 | 举例 |
---|---|---|
getBy | 根据字段的值查询数据 | 例如,getByName,getByEmail |
getFieldBy | 根据字段查询并返回某个字段的值 | 例如,getFieldByName |
$user = $User->getByName('liu21st');
$user = $User->getByEmail('liu21st@gmail.com');
$user = $User->getByAddress('中国深圳');
复制代码
$userId = $User->getFieldByName('liu21st','id');
复制代码
// 首先构造子查询SQL
$subQuery = $model->field('id,name')->table('tablename')->group('field')->where($where)->order('status')->select(false);
复制代码
$subQuery = $model->field('id,name')->table('tablename')->group('field')->where($where)->order('status')->buildSql();
复制代码
// 利用子查询进行查询
$model->table($subQuery.' a')->where()->order()->select()
复制代码
$warehouseProduct = D('WarehouseProductList');
$map = array();
$map['productId'] = $productId;
$map['void'] = 'N';
$product = $warehouseProduct->where($map)->find();
echo '执行的SQL: '.$warehouseProduct->getLastSql();
dump($product);
复制代码
复制代码
复制代码
复制代码
复制代码
复制代码
复制代码
复制代码
复制代码
$map['id'] = array(array('gt',1),array('lt',10)) ;
复制代码
复制代码
复制代码
复制代码
复制代码
复制代码
if($Data->execute('DROP TABLE IF EXISTS `think_form`;')){
$this->data .='<br>删除think_form完成</br>';
}else{
$this->data .='<br>删除think_form失败</br>';
}
复制代码
复制代码
复制代码
复制代码
if($Data->execute('DROP TABLE IF EXISTS `think_form`;')){
$this->data .='<br>删除think_form完成</br>';
}else{
$this->data .='<br>删除think_form失败</br>';
}
复制代码
复制代码
复制代码