• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ThinkPHP查询语句与关联查询用法实例_PHP

php 搞代码 4年前 (2022-01-25) 17次浏览 已收录 0个评论

ThinkPHP

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

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

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

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

$User = M("User"); //实例化User对象

$condition[‘name’] = ‘thinkphp’; // 把查询条件传入查询方法
$User->where($condition)->select();

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

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

带where条件的普通查询

1、字符串形式

$user=M('user');<br />$list=$user->where('id>5 and idselect();<br />$list=$user->where($data)->select();

2、数组形式

$user=M('user');<br />$list=$user->where(array('username'=>'www.bitsCN.com'))->select();<br />$list=$user->where($data)->select();

3、对象形式

$user=M('user');<br />$a=new stdClass();<br />$a->username='www.bitsCN.com;<br />$list=$user->where($a)->select();

两个表的关联查询:

$M_shopping = M('Shops'); <br />$M_product = M('Product'); <br />$list_shops = $M_shopping->join('<span>@本文来*源gaodai#ma#com搞*!代#%^码$网*</span><textarea>搞gaodaima代码</textarea>as shops left join hr_product as product on shops.product_id = product.p_id') <br />->field('product.p_id,product.p_name,shops.product_amount,shops.product_id') <br />->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'") <br />->group('shops.id') <br />->select();

区间查询

$user=M('user');<br />$data['id']=array(array('gt',20),array('lt',23),'and');<br />$list=$user->where($data)->select();

组合查询

$user=M('user');<br />$data['username']='pengyanjie';<br />$data['password']=array('eq','pengyanjie');<br />$data['id']=array('lt',30);<br />$data['_logic']='or';<br />$list=$user->where($data)->select();<br />dump($list);

复合查询

$user=M('user');<br />$data['username']=array('eq','pengyanjie');<br />$data['password']=array('like','p%');<br />$data['_logic']='or';<br />$where['_complex']=$where;<br />$where['id']=array('lt',30);<br />$list=$user->where($data)->select();

三个数据表的关联查询

$M_shopping = M('Shops');<br />$M_product = M('Product');<br />$M_proimg = M('Product_image');<br />$list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id left join<br />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,<br />product.p_procolor,product.p_price,product_amount*p_price as totalone')->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'")<br />->group('shops.id')->select();

数据表的查询条件

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

$m_test = M("Product");<br />$productmeaage = $m_test->where("p_id='$proid'")->select();

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

$M_product = M('Product');<br />$map['pid'] = $proid;<br />$p_result = $M_product->where($map)->select();

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ThinkPHP查询语句与关联查询用法实例_PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址