便利贴女孩插曲,boyfriend伴奏,引人注目的近义词
本文实例讲述了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框架程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
-
-
Yii框架应用组件用法实例分析
本文实例讲述了yii框架应用组件用法。分享给大家供大家参考,具体如下:应用组件 应用主体是, 它部署一组提供各种不同功能的 应用组件 来处理请求。 例如,url...
[阅读全文]
-
-
-
-
-
Yii 框架入口脚本示例分析
本文实例讲述了yii 框架入口脚本。分享给大家供大家参考,具体如下:入口脚本入口脚本是应用启动流程中的第一环, 一个应用(不管是网页应用还是控制台应用)只有一个...
[阅读全文]
-
PHP强制转化的形式整理
php强制转化有几种形式php强制转化有3种形式:1、在要转换的变量之前加上用括号括起来的目标类型;2、使用转强函数,如“intval()”,“strval()...
[阅读全文]
-
php7 新增功能实例总结
本文实例总结了php7 新增功能。分享给大家供大家参考,具体如下:一、核心 增加了group use语法声明。rfc: https://wiki.php.ne...
[阅读全文]
网友评论