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

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

运行 ThinkPHP 项目需要:安装 Composer;使用 Composer 创建项目;进入项目目录,执行 php bin/console serve;访问 http://localhost:8000 查看欢迎页面。

ThinkPHP 拥有多个版本,针对不同 PHP 版本而设计。主要版本包括 3.2、5.0、5.1 和 6.0,而次要版本用于修复 bug 和提供新功能。当前最新稳定版本为 ThinkPHP 6.0.16。在选择版本时,需考虑 PHP 版本、功能需求和社区支持。建议使用最新稳定版本以获得最佳性能和支持。

12306订票app下载最新版是一款大家非常满意的出行购票软件,想去哪里就去那里非常方便,软件内提供的票源非常多,只需要通过实名认证就能在线购票,所有用户的出行车票机票都可以轻松买到,享受不同的优惠折扣。还能提前开启预约抢票,预约酒店、专车接送都是可以的,有了它想去哪里就去那里一键购票,出行更加简单方便,让大家的出行体验更舒服,现在小编在线详细为12306用户们带来查看历史购票记录的方法。 1.打开铁路12306,点击右下角我的,点击我的订单 2.在订单页面点击已支付。 3.在已支付页

学信网如何查询自己的学历?在学信网中是可以查询到自己的学历,很多用户都不知道如何在学信网中查询到自己的学历,接下来就是小编为用户带来的学信网查询自己学历方法图文教程,感兴趣的用户快来一起看看吧!学信网使用教程学信网如何查询自己的学历一、学信网入口:https://www.chsi.com.cn/二、网站查询:第一步:点击上方学信网地址,进入首页点击【学历查询】;第二步:在最新的网页中点击如下图箭头所示的【查询】;第三步:之后在新页面点击【的登陆学信档案】;第四步:在登陆页面输入信息点击【登陆】;

ThinkPHP Framework 的本地运行步骤:下载并解压 ThinkPHP Framework 到本地目录。创建虚拟主机(可选),指向 ThinkPHP 根目录。配置数据库连接参数。启动 Web 服务器。初始化 ThinkPHP 应用程序。访问 ThinkPHP 应用程序 URL 运行。

Laravel 和 ThinkPHP 框架的性能比较:ThinkPHP 性能通常优于 Laravel,专注于优化和缓存。Laravel 性能良好,但对于复杂应用程序,ThinkPHP 可能更适合。

MySQL与PL/SQL是两种不同的数据库管理系统,分别代表了关系型数据库和过程化语言的特点。本文将比较MySQL和PL/SQL的异同点,并附带具体的代码示例进行说明。MySQL是一种流行的关系型数据库管理系统,采用结构化查询语言(SQL)来管理和操作数据库。而PL/SQL是Oracle数据库特有的过程化语言,用于编写存储过程、触发器和函数等数据库对象。相同

ThinkPHP 安装步骤:准备 PHP、Composer、MySQL 环境。使用 Composer 创建项目。安装 ThinkPHP 框架及依赖项。配置数据库连接。生成应用代码。启动应用并访问 http://localhost:8000。
