ThinkPHP查詢語(yǔ)句與關(guān)聯(lián)查詢用法實(shí)例
本文實(shí)例講述了ThinkPHP查詢語(yǔ)句與關(guān)聯(lián)查詢用法。分享給大家供大家參考。具體如下:
在thinkphp框架頁(yè)面中我們可以直接拼寫sql查詢語(yǔ)句來(lái)實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢讀寫操作,下面就對(duì)此加以實(shí)例說(shuō)明。
普通查詢除了字符串查詢條件外,數(shù)組和對(duì)象方式的查詢條件是非常常用的,這些是基本查詢所必須掌握的。
一、使用數(shù)組作為查詢條件
$condition['name'] = 'thinkphp'; // 把查詢條件傳入查詢方法
$User->where($condition)->select();
二、使用對(duì)象方式來(lái)查詢 可以使用任何對(duì)象 這里以stdClass內(nèi)置對(duì)象為例
// 定義查詢條件 $condition = new stdClass();
$condition->name = 'thinkphp'; // 查詢name的值為thinkphp的記錄
$User->where($condition)->select(); // 上面的查詢條件等同于 where('name="thinkphp"') 使用對(duì)象方式查詢和使用數(shù)組查詢的效果是相同的,并且是可
帶where條件的普通查詢
1、字符串形式
$list=$user->where('id>5 and id<9')->select();
$list=$user->where($data)->select();
2、數(shù)組形式
$list=$user->where(array('username'=>'www.dbjr.com.cn'))->select();
$list=$user->where($data)->select();
3、對(duì)象形式
$a=new stdClass();
$a->username='www.dbjr.com.cn;
$list=$user->where($a)->select();
兩個(gè)表的關(guān)聯(lián)查詢:
$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ū)間查詢
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();
組合查詢
$data['username']='pengyanjie';
$data['password']=array('eq','pengyanjie');
$data['id']=array('lt',30);
$data['_logic']='or';
$list=$user->where($data)->select();
dump($list);
復(fù)合查詢
$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();
三個(gè)數(shù)據(jù)表的關(guān)聯(lián)查詢
$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中,這樣就方便了條件的書(shū)寫
$productmeaage = $m_test->where("p_id='$proid'")->select();
② 除了上面的方法還有一種是以數(shù)組的方式
$map['pid'] = $proid;
$p_result = $M_product->where($map)->select();
希望本文所述對(duì)大家的ThinkPHP框架程序設(shè)計(jì)有所幫助。
- ThinkPHP多表聯(lián)合查詢的常用方法
- thinkphp實(shí)現(xiàn)like模糊查詢實(shí)例
- ThinkPHP視圖查詢?cè)斀?/a>
- ThinkPHP查詢返回簡(jiǎn)單字段數(shù)組的方法
- thinkphp數(shù)據(jù)查詢和遍歷數(shù)組實(shí)例
- thinkphp學(xué)習(xí)筆記之多表查詢
- ThinkPHP5查詢數(shù)據(jù)及處理結(jié)果的方法小結(jié)
- ThinkPHP中的常用查詢語(yǔ)言匯總
- ThinkPHP采用GET方式獲取中文參數(shù)查詢無(wú)結(jié)果的解決方法
- Thinkphp使用mongodb數(shù)據(jù)庫(kù)實(shí)現(xiàn)多條件查詢方法
- thinkphp視圖模型查詢提示ERR: 1146:Table ''db.pr_order_view'' doesn''t exist的解決方法
- thinkPHP5實(shí)現(xiàn)的查詢數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)實(shí)例
- thinkphp中多表查詢中防止數(shù)據(jù)重復(fù)的sql語(yǔ)句(必看)
- ThinkPHP中關(guān)聯(lián)查詢實(shí)例
- thinkPHP實(shí)現(xiàn)多字段模糊匹配查詢的方法
- ThinkPHP5聯(lián)合(關(guān)聯(lián))查詢、多條件查詢與聚合查詢實(shí)例詳解
- thinkphp多表查詢兩表有重復(fù)相同字段的完美解決方法
- ThinkPHP like模糊查詢,like多匹配查詢,between查詢,in查詢,一般查詢書(shū)寫方法
相關(guān)文章
Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法
這篇文章主要介紹了Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法,涉及Codeigniter操作XML文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03