這篇文章主要介紹了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 id<9')->select(); $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();
以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!
相關建議:
#
以上是ThinkPHP的查詢語句與關聯查詢的用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

到Improveyourphpwebsite的實力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)優化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

是的,ItispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailsending.2)enasledeLaysBetenemailstoavoidSpamflagssspamflags.3))

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

使用PHP發送電子郵件的最佳方法包括:1.使用PHP的mail()函數進行基本發送;2.使用PHPMailer庫發送更複雜的HTML郵件;3.使用SendGrid等事務性郵件服務提高可靠性和分析能力。通過這些方法,可以確保郵件不僅到達收件箱,還能吸引收件人。

計算PHP多維數組的元素總數可以使用遞歸或迭代方法。 1.遞歸方法通過遍歷數組並遞歸處理嵌套數組來計數。 2.迭代方法使用棧來模擬遞歸,避免深度問題。 3.array_walk_recursive函數也能實現,但需手動計數。

在PHP中,do-while循環的特點是保證循環體至少執行一次,然後再根據條件決定是否繼續循環。 1)它在條件檢查之前執行循環體,適合需要確保操作至少執行一次的場景,如用戶輸入驗證和菜單系統。 2)然而,do-while循環的語法可能導致新手困惑,且可能增加不必要的性能開銷。

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函數進行快速哈希,但不適合密碼存儲。 2.使用sha256函數提高安全性。 3.使用password_hash函數處理密碼,提供最高安全性和便捷性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境

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