Rumah > pembangunan bahagian belakang > tutorial php > ThinkPHP的查询语句与关联查询的用法

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

不言
Lepaskan: 2023-03-30 17:08:02
asal
1559 orang telah melayarinya

这篇文章主要介绍了ThinkPHP查询语句与关联查询用法,以实例的形式常见的查询方法,包括数组作为查询条件及对象方式来查询等技巧,需要的朋友可以参考下

本文实例讲述了ThinkPHP查询语句与关联查询用法。分享给大家供大家参考。具体如下:

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

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

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

$User = M("User"); //实例化User对象
$condition['name'] = 'thinkphp'; // 把查询条件传入查询方法
$User->where($condition)->select();
Salin selepas log masuk

二、使用对象方式来查询 可以使用任何对象 这里以stdClass内置对象为例

$User = M("User"); // 实例化User对象
// 定义查询条件 $condition = new stdClass();
$condition->name = 'thinkphp';  // 查询name的值为thinkphp的记录
$User->where($condition)->select(); //  上面的查询条件等同于 where('name="thinkphp"') 使用对象方式查询和使用数组查询的效果是相同的,并且是可
带where条件的普通查询
Salin selepas log masuk

1、字符串形式

$user=M('user');
$list=$user->where(&#39;id>5 and id<9&#39;)->select();
$list=$user->where($data)->select();
Salin selepas log masuk

2、数组形式

$user=M(&#39;user&#39;);
$list=$user->where(array(&#39;username&#39;=>&#39;www.jb51.net&#39;))->select();
$list=$user->where($data)->select();
Salin selepas log masuk

3、对象形式

$user=M(&#39;user&#39;);
$a=new stdClass();
$a->username=&#39;www.jb51.net;
$list=$user->where($a)->select();
Salin selepas log masuk

两个表的关联查询:

$M_shopping = M(&#39;Shops&#39;); 
$M_product = M(&#39;Product&#39;); 
$list_shops = $M_shopping->join(&#39;as shops left join hr_product as product on shops.product_id = product.p_id&#39;) 
->field(&#39;product.p_id,product.p_name,shops.product_amount,shops.product_id&#39;) 
->where("shops.user_cookie=&#39;".$_COOKIE[&#39;hr_think_userid&#39;]."&#39;") 
->group(&#39;shops.id&#39;) 
->select();
Salin selepas log masuk

区间查询

$user=M(&#39;user&#39;);
$data[&#39;id&#39;]=array(array(&#39;gt&#39;,20),array(&#39;lt&#39;,23),&#39;and&#39;);
$list=$user->where($data)->select();
Salin selepas log masuk

组合查询

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=&#39;pengyanjie&#39;;
$data[&#39;password&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;id&#39;]=array(&#39;lt&#39;,30);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$list=$user->where($data)->select();
dump($list);
Salin selepas log masuk

复合查询

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;password&#39;]=array(&#39;like&#39;,&#39;p%&#39;);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$where[&#39;_complex&#39;]=$where;
$where[&#39;id&#39;]=array(&#39;lt&#39;,30);
$list=$user->where($data)->select();
Salin selepas log masuk

三个数据表的关联查询

$M_shopping = M(&#39;Shops&#39;);
$M_product = M(&#39;Product&#39;);
$M_proimg = M(&#39;Product_image&#39;);
$list_shops = $M_shopping->join(&#39;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&#39;)->fiel(&#39;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&#39;)->where("shops.user_cookie=&#39;".$_COOKIE[&#39;hr_think_userid&#39;]."&#39;")
->group(&#39;shops.id&#39;)->select();
Salin selepas log masuk

数据表的查询条件
① 下面的是直接吧查询的条件放到了where中,这样就方便了条件的书写

$m_test = M("Product");
$productmeaage = $m_test->where("p_id=&#39;$proid&#39;")->select();
Salin selepas log masuk

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

$M_product = M(&#39;Product&#39;);
$map[&#39;pid&#39;] = $proid;
$p_result = $M_product->where($map)->select();
Salin selepas log masuk

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

相关推荐:

ThinkPHP的关联模型

ThinkPHP中常用的查询语言

Atas ialah kandungan terperinci ThinkPHP的查询语句与关联查询的用法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Isu terkini
ThinkPHP Mengapa menggunakan komposer?
daripada 1970-01-01 08:00:00
0
0
0
thinkphp memuat naik fail
daripada 1970-01-01 08:00:00
0
0
0
Bagaimanakah Thinkphp memanggil sambungan PHP?
daripada 1970-01-01 08:00:00
0
0
0
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan