ThinkPHP多表查询处理
ThinkPHP多表连接查询处理
ThinkPHP关联查询(多表查询)
网上找到三种方法:table()、join()、原生SQL语句查询。(以下三种方法输出结果一致,并且很好的保留了ThinkPHP自己的分页功能)
第一种:table()方法
实例:需要连接查询两张表(表agent和表transinfo)
<code>$Model</code><code>=</code><code>new</code> <code>Model();</code>
<code>$sqlcount</code><code>=</code><code>"select count(*) as mycount from agent a ,transinfo t where t.clientId=a.id and t.transType like '%agent%' and a.id in ("</code><code>.</code><code>$agent_str</code><code>.</code><code>")"</code><code>;</code>
<code>$listCount</code> <code>= </code><code>$Model</code> <code>->query(</code><code>$sqlcount</code><code>);</code>
<code>$Page</code> <code>= </code><code>new</code> <code>Page ( </code><code>$listCount</code><code>[0][mycount], 2 );</code>
<code>$show</code> <code>= </code><code>$Page</code><code>->show ();</code>
<code>$list</code> <code>= </code><code>$Model</code><code>->table(</code><code>'agent a, transinfo t'</code><code>)->where(</code><code>"t.clientId=a.id and t.transType like '%agent%' and a.id in ("</code><code>.</code><code>$agent_str</code><code>.</code><code>")"</code><code>)->limit ( </code><code>$Page</code><code>->firstRow . </code><code>','</code> <code>. </code><code>$Page</code><code>->listRows )->select();</code>
<code>//echo $Model->getLastSql();</code>
<code>$this</code><code>->assign(</code><code>'list'</code><code>,</code><code>$list</code><code>);</code><code>// 赋值数据集</code>
<code>$this</code><code>->assign(</code><code>'page'</code><code>,</code><code>$show</code><code>);</code><code>// 赋值分页输出</code>
第二种:join()方法
<code>$agentModel</code> <code>= </code><code>$Model</code><code>->Table(</code><code>"agent"</code><code>);</code>
<code>$listCount</code> <code>= </code><code>$agentModel</code><code>->join(</code><code>" AS a RIGHT JOIN transinfo t ON t.clientId=a.id and t.transType like '%agent%' and a.id in ("</code><code>.</code><code>$agent_str</code><code>.</code><code>")"</code><code>)</code>
<code> </code><code>->field(</code><code>"count(*) as mycount"</code><code>)</code>
<code> </code><code>->select();</code>
<code>$list</code> <code>= </code><code>$agentModel</code><code>->join(</code><code>" AS a RIGHT JOIN transinfo t ON t.clientId=a.id and t.transType like '%agent%' and a.id in ("</code><code>.</code><code>$agent_str</code><code>.</code><code>") order by t.id DESC limit "</code><code>.</code><code>$Page</code><code>->firstRow.</code><code>","</code><code>.</code><code>$Page</code><code>->listRows)</code>
<code> </code><code>->field(</code><code>"a.*,t.*"</code><code>)</code>
<code>//echo $agentModel->getLastSql();</code>
提示:你也可以这样实例化更简洁(官方推荐的):$agentModel = M('Agent'); // 实例化User对象
第三种:原生SQL语句查询法
<code>$sqlcount</code><code>=</code><code>"select count(*) as mycount from agent a ,transinfo t where t.clientId=a.id "</code><code>.</code><code>$where</code><code>. </code><code>" and t.transType like '%agent%' and a.id in ("</code><code>.</code><code>$agent_str</code><code>.</code><code>")"</code><code>;</code>
<code>$Page</code> <code>= </code><code>new</code> <code>Page(</code><code>$listCount</code><code>[0][</code><code>'mycount'</code><code>],2);</code><code>// 实例化分页类 传入总记录数和每页显示的记录数</code>
<code>$show</code> <code>= </code><code>$Page</code><code>->show();</code>
<code>$sqlList</code><code>=</code><code>"select t.*,a.* from agent a ,transinfo t where t.clientId=a.id "</code><code>.</code><code>$where</code><code>. </code><code>" and t.transType like '%agent%' and a.id in ("</code><code>.</code><code>$agent_str</code><code>.</code><code>") limit {$Page->firstRow},{$Page->listRows}"</code><code>;</code>
<code>$list</code> <code>= </code><code>$Model</code> <code>->query(</code><code>$sqlList</code><code>);</code>
参考资料:
<a href="http://hi.baidu.com/wjlhh001/item/18d9a91031081b5e2a3e22af" target="_blank">http://hi.baidu.com/wjlhh001/item/18d9a91031081b5e2a3e22af</a>
<a href="http://www.yunda51.com/304.html" target="_blank">http://www.yunda51.com/304.html</a>
<a href="http://www.w3school.com.cn/sql/sql_join_right.asp" target="_blank">http://www.w3school.com.cn/sql/sql_join_right.asp</a>
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1564492,如需转载请自行联系原作者