php关联查询和循环查询语句,ThinkPHP查询语句与关联查询用法实例

搜索热词

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

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

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

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

代码如下:

$condition['name'] = 'thinkPHP'; // 把查询条件传入查询方法

$User->where($condition)->select();

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

代码如下:

name = 'thinkPHP'; // 查询name的值为thinkPHP的记录

$User->where($condition)->select(); // 上面的查询条件等同于 where('name="thinkPHP"') 使用对象方式查询和使用数组查询的效果是相同的,并且是可

带where条件的普通查询

1、字符串形式

代码如下:

where('id>5 and id<9')->select();

$list=$user->where($data)->select();

2、数组形式

代码如下:

where(array('username'=>'www.jb51.cc'))->select();

$list=$user->where($data)->select();

3、对象形式

代码如下:

username='www.jb51.cc;

$list=$user->where($a)->select();

两个表的关联查询:

代码如下:

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

区间查询

代码如下:

where($data)->select();

组合查询

代码如下:

where($data)->select();

dump($list);

复合查询

代码如下:

where($data)->select();

三个数据表的关联查询

代码如下:

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,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中,这样就方便了条件的书写

代码如下:

where("p_id='$proid'")->select();

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

代码如下:

where($map)->select();

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

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值