欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年11月01日 11:42:26   投稿:shichen2014  
這篇文章主要介紹了ThinkPHP查詢語(yǔ)句與關(guān)聯(lián)查詢用法,以實(shí)例的形式常見(jiàn)的查詢方法,包括數(shù)組作為查詢條件及對(duì)象方式來(lái)查詢等技巧,需要的朋友可以參考下

本文實(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ù)組作為查詢條件

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

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


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

復(fù)制代碼 代碼如下:
$User = M("User"); // 實(shí)例化User對(duì)象
// 定義查詢條件 $condition = new stdClass();
$condition->name = 'thinkphp';  // 查詢name的值為thinkphp的記錄
$User->where($condition)->select(); //  上面的查詢條件等同于 where('name="thinkphp"') 使用對(duì)象方式查詢和使用數(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'=>'www.dbjr.com.cn'))->select();
$list=$user->where($data)->select();

3、對(duì)象形式

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

兩個(gè)表的關(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();

三個(gè)數(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中,這樣就方便了條件的書(shū)寫

復(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();

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

相關(guān)文章

  • 小程序微信支付功能配置方法示例詳解【基于thinkPHP】

    小程序微信支付功能配置方法示例詳解【基于thinkPHP】

    這篇文章主要介紹了小程序微信支付功能配置方法,結(jié)合實(shí)例形式分析了基于thinkPHP的微信小程序支付功能相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-05-05
  • PHP反向代理類代碼

    PHP反向代理類代碼

    這篇文章主要介紹了PHP反向代理類代碼,需要的朋友可以參考下
    2014-08-08
  • yii2 url重寫并隱藏index.php方法

    yii2 url重寫并隱藏index.php方法

    這篇文章主要介紹了yii2 url重寫并隱藏index.php方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • thinkphp模板繼承實(shí)例簡(jiǎn)述

    thinkphp模板繼承實(shí)例簡(jiǎn)述

    這篇文章主要介紹了thinkphp模板繼承的實(shí)現(xiàn)方法,以一個(gè)簡(jiǎn)單實(shí)例的形式簡(jiǎn)述了ThinkPHP中父級(jí)模板與對(duì)應(yīng)的繼承方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • php截取中文字符串不亂碼的方法

    php截取中文字符串不亂碼的方法

    利用php內(nèi)置方法mb_substr截取中文不亂碼,使用起來(lái)非常簡(jiǎn)單,大家參考使用吧
    2013-12-12
  • php 三元運(yùn)算符實(shí)例詳細(xì)介紹

    php 三元運(yùn)算符實(shí)例詳細(xì)介紹

    php中三元運(yùn)算符又被叫做三目運(yùn)算符了,其實(shí)我常把它叫作問(wèn)號(hào)運(yùn)行符其實(shí)都可以這樣做,三元運(yùn)算符可以實(shí)現(xiàn)簡(jiǎn)單的條件判斷功能,下在我來(lái)給各位介紹一些三元運(yùn)算符的例子
    2016-12-12
  • Yii框架安裝簡(jiǎn)明教程

    Yii框架安裝簡(jiǎn)明教程

    這篇文章主要介紹了Yii框架安裝方法,總結(jié)分析了Yii框架安裝的基本步驟、命令與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法

    Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法

    這篇文章主要介紹了Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法,涉及Codeigniter操作XML文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • php中smarty區(qū)域循環(huán)的方法

    php中smarty區(qū)域循環(huán)的方法

    這篇文章主要介紹了php中smarty區(qū)域循環(huán)的方法,實(shí)例分析了smarty中foreach循環(huán)與section循環(huán)的使用技巧,需要的朋友可以參考下
    2015-06-06
  • PHP 微信支付類 demo

    PHP 微信支付類 demo

    這篇文章主要介紹了PHP 微信支付類 demo的相關(guān)資料,需要的朋友可以參考下
    2015-11-11

最新評(píng)論