ThinkPHP查询语句与关联查询的使用方法

发布时间:2021-07-02 15:53:28 作者:chen
来源:亿速云 阅读:270

这篇文章主要介绍“ThinkPHP查询语句与关联查询的使用方法”,在日常操作中,相信很多人在ThinkPHP查询语句与关联查询的使用方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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查询语句与关联查询的使用方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. MySQL 查询语句select讲解与练习
  2. 条件语句与查询语句

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

thinkphp

上一篇:Python中如何使用赋值运算符

下一篇:Python中如何使用条件运算符

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》