thinkphp left joinjoin查询的一些小问题

ThinkPHP&query原生关联查询&left&join
thinkphp提供了join方法来实现关联查询,但是很不好用,还是用原生的方便,所以,推荐大家用query方法。
$Model = new Model();
&$sql = "SELECT a.id, a.attr_name,
a.attr_input_type, a.attr_type, a.attr_values, v.attr_value,
v.attr_price ".
&"FROM hh_typeattr AS a ".
&"LEFT JOIN hh_siteattr AS v ".
&"ON v.attr_id = a.id AND v.site_id = '$site_id'
&"WHERE a.type_id = " . intval($type_id) ." OR
a.type_id = 0 ".
&"ORDER BY a.listorder, a.attr_type, a.id,
v.attr_price, v.id";
&$row = $Model-&query($sql);
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。ThinkPHP多表联合查询的常用方法
转载 & & 投稿:shichen2014
这篇文章主要介绍了ThinkPHP多表联合查询的常用方法,对于项目开发非常重要!需要的朋友可以参考下
ThinkPHP中关联查询(即多表联合查询)可以使用 table() 方法或和join方法,具体使用如下例所示:
1、原生查询示例:
代码如下:$Model = new Model();
$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p-&firstRow.','.$p-&listR
$voList = $Model-&query($sql);
2、join()方法示例:
代码如下:$user = new Model('user');
$list = $user-&join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' );
3、table()方法示例:
代码如下:$list = $user-&table('user_status stats, user_profile profile')-&where('stats.id = profile.typeid')-&field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')-&order('stats.id desc' )-&select();
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具ThinkPHP查询语句与关联查询用法实例
转载 & & 投稿:shichen2014
这篇文章主要介绍了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.
$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['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'] = $
$p_result = $M_product-&where($map)-&select();
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具1111人阅读
thinkphp(34)
1、原生查询
$Model = new Model();
$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p-&firstRow.','.$p-&listR
$voList = $Model-&query($sql);
$user = new Model('user');
$list = $user-&join('RIGHT JOIN user_profile&ON user_stats.id = user_profile.typeid' );
3、table()
$list = $user-&table('user_status stats, user_profile&profile')-&where('stats.id = profile.typeid')-&field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')-&order('stats.id desc' )-&select();
原文地址:http://blog.sina.com.cn/s/blog_ad5cgn.html
访问:78746次
排名:千里之外
原创:27篇
转载:38篇
(2)(1)(1)(1)(1)(6)(1)(7)(4)(1)(5)(6)(7)(1)(1)(4)(16)他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 thinkphp3.2 join查询 的文章

 

随机推荐