本文实例讲述了ThinkPHP查询语句与关联查询用法。分享给大家供大家参考。具体如下:
在thinkphp框架页面中我们可以直接拼写sql查询语句来实现数据库查询读写操作,下面就对此加以实例说明。
普通查询除了字符串查询条件外,数组和对象方式的查询条件是非常常用的,这些是基本查询所必须掌握的。
一、使用数组作为查询条件
复制代码 代码如下:$User = M("User"); //实例化User对象
$condition['name'] = 'thinkphp'; // 把查询条件传入查询方法
$User->where($condition)->select();
二、使用对象方式来查询 可以使用任何对象 这里以stdClass内置对象为例
复制代码 代码如下:$User = M("User"); // 实例化User对象
// 定义查询条件 $condition = new stdClass();
$condition->name = 'thinkphp'; // 查询name的值为thinkphp的记录
$User->where($condition)->select(); // 上面的查询条件等同于 where('name="thinkphp"') 使用对象方式查询和使用数组查询的效果是相同的,并且是可
带where条件的普通查询
1、字符串形式
复制代码 代码如下:$user=M('user');
$list=$user->where('id>5 and id<9')->select();
$list=$user->where($data)->select();
2、数组形式
复制代码 代码如下:$user=M('user');
$list=$user->where(array('username'=>'www.jb51.net'))->select();
$list=$user->where($data)->select();
3、对象形式
复制代码 代码如下:$user=M('user');
$a=new stdClass();
$a->username='www.jb51.net;
$list=$user->where($a)->select();
两个表的关联查询:
复制代码 代码如下:$M_shopping = M('Shops');
$M_product = M('Product');
$list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id')
->field('product.p_id,product.p_name,shops.product_amount,shops.product_id')
->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'")
->group('shops.id')
->select();
区间查询
复制代码 代码如下:$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();
组合查询
复制代码 代码如下:$user=M('user');
$data['username']='pengyanjie';
$data['password']=array('eq','pengyanjie');
$data['id']=array('lt',30);
$data['_logic']='or';
$list=$user->where($data)->select();
dump($list);
复合查询
复制代码 代码如下:$user=M('user');
$data['username']=array('eq','pengyanjie');
$data['password']=array('like','p%');
$data['_logic']='or';
$where['_complex']=$where;
$where['id']=array('lt',30);
$list=$user->where($data)->select();
三个数据表的关联查询
复制代码 代码如下:$M_shopping = M('Shops');
$M_product = M('Product');
$M_proimg = M('Product_image');
$list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id left join
hr_product_image as productimgon productimg.p_id = product.p_id')->fiel('productimg.pi_url,product.p_id,product.p_name,shops.product_amount,shops.product_id,product.am_id,
product.p_procolor,product.p_price,product_amount*p_price as totalone')->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'")
->group('shops.id')->select();
数据表的查询条件
① 下面的是直接吧查询的条件放到了where中,这样就方便了条件的书写
复制代码 代码如下:$m_test = M("Product");
$productmeaage = $m_test->where("p_id='$proid'")->select();
② 除了上面的方法还有一种是以数组的方式
复制代码 代码如下:$M_product = M('Product');
$map['pid'] = $proid;
$p_result = $M_product->where($map)->select();
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]