国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

ThinkPHP查詢語句與關(guān)聯(lián)查詢用法實(shí)例

2024-05-04 23:26:37
字體:
供稿:網(wǎng)友
這篇文章主要介紹了ThinkPHP查詢語句與關(guān)聯(lián)查詢用法,以實(shí)例的形式常見的查詢方法,包括數(shù)組作為查詢條件及對象方式來查詢等技巧,需要的朋友可以參考下
 
 

本文實(shí)例講述了ThinkPHP查詢語句與關(guān)聯(lián)查詢用法。分享給大家供大家參考。具體如下:

在thinkphp框架頁面中我們可以直接拼寫sql查詢語句來實(shí)現(xiàn)數(shù)據(jù)庫查詢讀寫操作,下面就對此加以實(shí)例說明。

普通查詢除了字符串查詢條件外,數(shù)組和對象方式的查詢條件是非常常用的,這些是基本查詢所必須掌握的。

一、使用數(shù)組作為查詢條件

復(fù)制代碼代碼如下:
$User = M("User"); //實(shí)例化User對象

 

$condition['name'] = 'thinkphp'; // 把查詢條件傳入查詢方法
$User->where($condition)->select();


二、使用對象方式來查詢 可以使用任何對象 這里以stdClass內(nèi)置對象為例

 

 

復(fù)制代碼代碼如下:
$User = M("User"); // 實(shí)例化User對象
// 定義查詢條件 $condition = new stdClass();
$condition->name = 'thinkphp';  // 查詢name的值為thinkphp的記錄
$User->where($condition)->select(); //  上面的查詢條件等同于 where('name="thinkphp"') 使用對象方式查詢和使用數(shù)組查詢的效果是相同的,并且是可

 

帶where條件的普通查詢
   
1、字符串形式

 

復(fù)制代碼代碼如下:
$user=M('user');
$list=$user->where('id>5 and id<9')->select();
$list=$user->where($data)->select();

2、數(shù)組形式

 

 

復(fù)制代碼代碼如下:
$user=M('user');
$list=$user->where(array('username'=>'m.survivalescaperooms.com'))->select();
$list=$user->where($data)->select();

3、對象形式

 

 

復(fù)制代碼代碼如下:
$user=M('user');
$a=new stdClass();
$a->username='m.survivalescaperooms.com;
$list=$user->where($a)->select();

兩個表的關(guān)聯(lián)查詢:

 

 

復(fù)制代碼代碼如下:
$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();

 

區(qū)間查詢

 

復(fù)制代碼代碼如下:
$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();

組合查詢

 

 

復(fù)制代碼代碼如下:
$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);

復(fù)合查詢

 

 

復(fù)制代碼代碼如下:
$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();

三個數(shù)據(jù)表的關(guān)聯(lián)查詢

 

 

復(fù)制代碼代碼如下:
$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();

數(shù)據(jù)表的查詢條件

① 下面的是直接吧查詢的條件放到了where中,這樣就方便了條件的書寫

 

 

復(fù)制代碼代碼如下:
$m_test = M("Product");
$productmeaage = $m_test->where("p_id='$proid'")->select();

 

② 除了上面的方法還有一種是以數(shù)組的方式

 

復(fù)制代碼代碼如下:
$M_product = M('Product');
$map['pid'] = $proid;
$p_result = $M_product->where($map)->select();

 

希望本文所述對大家的ThinkPHP框架程序設(shè)計有所幫助。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 光山县| 平谷区| 泸西县| 长白| 海伦市| 岱山县| 韩城市| 基隆市| 民权县| 宝鸡市| 察隅县| 芜湖市| 蒙自县| 方山县| 永平县| 上高县| 崇州市| 石景山区| 丹巴县| 五河县| 连城县| 晋城| 花垣县| 明星| 揭西县| 德令哈市| 天长市| 贡觉县| 吉木萨尔县| 喜德县| 灵武市| 江津市| 锡林郭勒盟| 建平县| 汉沽区| 孟津县| 舞阳县| 师宗县| 湘潭市| 周至县| 太仆寺旗|