最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
thinkphp查询语句与关联查询的用法介绍
时间:2022-06-25 02:30:44 编辑:袖梨 来源:一聚教程网
普通查询除了字符串查询条件外,数组和对象方式的查询条件是非常常用的,这些是基本查询所必须掌握的。
一、使用数组作为查询条件 $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'); |
|
2、数组形式
| 代码如下 | 复制代码 |
|
$user=M('user'); |
|
3、对象形式
| 代码如下 | 复制代码 |
|
$user=M('user'); |
|
两个表的关联查询:
| 代码如下 | 复制代码 |
|
$M_shopping = M('Shops'); |
|
区间查询
| 代码如下 | 复制代码 |
|
$user=M('user'); |
|
组合查询
| 代码如下 | 复制代码 |
|
$user=M('user'); |
|
复合查询
| 代码如下 | 复制代码 |
|
$user=M('user'); |
|
三个数据表的关联查询
| 代码如下 | 复制代码 |
|
$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(); |
|
数据表的查询条件
1:下面的是直接吧查询的条件放到了where中,这样就方便了条件的书写
| 代码如下 | 复制代码 |
|
$m_test = M("Product"); $productmeaage = $m_test->where("p_id='$proid'")->select(); |
|
2:除了上面的方法还有一种是以数组的方式
| 代码如下 | 复制代码 |
|
$M_product = M('Product'); $map['pid'] = $proid; $p_result = $M_product->where($map)->select(); |
|
相关文章
- 污污漫画入口风险警示-合规访问指南与安全指引 12-13
- 海棠文学城网页版官网入口-2025在线网址直达 12-13
- 土豪漫画官方APP免费下载入口-正版无广告畅读保障 12-13
- 歪歪漫画app最新版下载入口-首页畅读无广告直接进 12-13
- SkrBT磁力官网最新入口-skrbt磁力引擎极速版入口 12-13
- 微博热搜网页版极速入口-微博热搜榜官方直达链接 12-13