ThinPHP中Join实用举例

这里使用了两个表,第一个表为Post表,第二个表为Category表。

MYSQL语句为:

SELECT count(*) AS total_num, a.cate_id, b.name FROM post a LEFT JOIN category b ON a.cate_id=b.id WHERE a.status='a' GROUP BY a.cate_id ORDER BY b.name 

ThinkPHP中Join的写法:

$blog = D("Post"); 
$blog->table('post a')->join("category b ON a.cate_id=b.id")->field('count(*) AS total_num, a.cate_id, t.name')->group("a.cate_id")->order('b.name')->where("a.status='a'" )->findall(); 

如果你在ThinkPHP中设置了表的前缀,那么在写Join的时候,表名也要加上前缀。

affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Comments are closed.