登录  /  注册

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

不言
发布: 2023-03-30 17:08:02
原创
938人浏览过

这篇文章主要介绍了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();
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

ThinkPHP的关联模型

ThinkPHP中常用的查询语言

以上就是ThinkPHP的查询语句与关联查询的用法的详细内容,更多请关注php中文网其它相关文章!

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学