加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 02:07:10 所属栏目:PHP教程 来源:网络整理
导读:《PHP实战:ThinkPHP查询语句与关联查询用法实例》要点: 本文介绍了PHP实战:ThinkPHP查询语句与关联查询用法实例,希望对您有用。如果有疑问,可以联系我们。 PHP实例 本篇章节讲解ThinkPHP查询语句与关联查询用法.供大家参考研究.具体如下: PHP

《PHP实战:ThinkPHP查询语句与关联查询用法实例》要点:
本文介绍了PHP实战:ThinkPHP查询语句与关联查询用法实例,希望对您有用。如果有疑问,可以联系我们。

PHP实例本篇章节讲解ThinkPHP查询语句与关联查询用法.分享给大家供大家参考.具体如下:

PHP实例在thinkphp框架页面中我们可以直接拼写sql查询语句来实现数据库查询读写操作,下面就对此加以实例说明.

PHP实例普通查询除了字符串查询条件外,数组和对象方式的查询条件是非常常用的,这些是基本查询所必须掌握的.

PHP实例一、使用数组作为查询条件

代码如下:
$User = M("User"); //实例化User对象

PHP实例$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"') 使用对象方式查询和使用数组查询的效果是相同的,并且是可

PHP实例带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.aspzz.cn'))->select();
$list=$user->where($data)->select();

3、对象形式
代码如下:
$user=M('user');
$a=new stdClass();
$a->username='www.aspzz.cn;
$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();

PHP实例区间查询

代码如下:
$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,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();

PHP实例② 除了上面的办法还有一种是以数组的方式

代码如下:
$M_product = M('Product');
$map['pid'] = $proid;
$p_result = $M_product->where($map)->select();

PHP实例希望本文所述对大家的ThinkPHP框架程序设计有所赞助.

编程之家培训学院每天发布《PHP实战:ThinkPHP查询语句与关联查询用法实例》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读