ThinkPHP查询语句与关联查询用法实例


Posted in PHP onNovember 01, 2014

本文实例讲述了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'=>'3water.com'))->select();

$list=$user->where($data)->select();

3、对象形式
$user=M('user');

$a=new stdClass();

$a->username='3water.com;

$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框架程序设计有所帮助。

PHP 相关文章推荐
一个odbc连mssql分页的类
Oct 09 PHP
杏林同学录(九)
Oct 09 PHP
php中将指针移动到数据集初始位置的实现代码[mysql_data_seek]
Nov 01 PHP
php对图像的各种处理函数代码小结
Jul 08 PHP
神盾加密解密教程(三)PHP 神盾解密工具
Jun 08 PHP
本地计算机无法启动Apache故障处理
Aug 08 PHP
PHP贪婪算法解决0-1背包问题实例分析
Mar 23 PHP
经典PHP加密解密函数Authcode()修复版代码
Apr 05 PHP
详解 PHP加密解密字符串函数附源码下载
Dec 18 PHP
yii2局部关闭(开启)csrf的验证的实例代码
Jul 10 PHP
PHP fclose函数用法总结
Feb 15 PHP
PHP基本语法
Mar 31 PHP
ThinkPHP分组下自定义标签库实例
Nov 01 #PHP
PHP根据两点间的经纬度计算距离
Oct 31 #PHP
ThinkPHP在新浪SAE平台的部署实例
Oct 31 #PHP
封装ThinkPHP的一个文件上传方法实例
Oct 31 #PHP
ThinkPHP无限级分类原理实现留言与回复功能实例
Oct 31 #PHP
ThinkPHP控制器间实现相互调用的方法
Oct 31 #PHP
ThinkPHP上使用多说评论插件的方法
Oct 31 #PHP
You might like
PHP imagecreatefrombmp 从BMP文件或URL新建一图像
2012/07/16 PHP
使用PHP获取汉字的拼音(全部与首字母)
2013/06/27 PHP
PHP开发框架kohana中处理ajax请求的例子
2014/07/14 PHP
PHP检测字符串是否为UTF8编码的常用方法
2014/11/21 PHP
php获取文章上一页与下一页的方法
2014/12/01 PHP
PHP基于PDO扩展操作mysql数据库示例
2018/12/24 PHP
JavaScript中的this实例分析
2011/04/28 Javascript
使用jQuery实现星级评分代码分享
2014/12/09 Javascript
Javascript中的Callback方法浅析
2015/03/15 Javascript
JavaScript实现的圆形浮动标签云效果实例
2015/08/06 Javascript
NodeJS的Promise的用法解析
2016/05/05 NodeJs
AngularJS 单元测试(二)详解
2016/09/21 Javascript
Swiper实现轮播图效果
2017/07/03 Javascript
浅析Angular19 自定义表单控件
2018/01/31 Javascript
nodejs简单抓包工具使用详解
2019/08/23 NodeJs
微信小程序实现单个卡片左滑显示按钮并防止上下滑动干扰功能
2019/12/06 Javascript
Vue前端判断数据对象是否为空的实例
2020/09/02 Javascript
Javascript 模拟mvc实现点餐程序案例详解
2020/12/24 Javascript
vue3中轻松实现switch功能组件的全过程
2021/01/07 Vue.js
[14:50]2018DOTA2亚洲邀请赛开幕式
2018/04/03 DOTA
Python进阶之递归函数的用法及其示例
2018/01/31 Python
python生成tensorflow输入输出的图像格式的方法
2018/02/12 Python
解决安装pyqt5之后无法打开spyder的问题
2019/12/13 Python
python计算导数并绘图的实例
2020/02/29 Python
Python sql注入 过滤字符串的非法字符实例
2020/04/03 Python
Python TestSuite生成测试报告过程解析
2020/07/23 Python
详解PyQt5中textBrowser显示print语句输出的简单方法
2020/08/07 Python
Django中和时区相关的安全问题详解
2020/10/12 Python
巴西箱包、背包、钱包和旅行配件购物网站:Inovathi
2019/12/14 全球购物
法人身份证明书
2014/10/08 职场文书
三年级学生评语大全
2014/12/26 职场文书
活着观后感
2015/06/03 职场文书
团队拓展训练感想
2015/08/07 职场文书
签证扫盲贴,41个常见签证知识,需要的拿走
2019/08/09 职场文书
Python 可迭代对象 iterable的具体使用
2021/08/07 Python
SpringDataJPA在Entity中常用的注解介绍
2021/12/06 Java/Android