欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Thinkphp join 连接查询,thinkphpjoin

程序员文章站 2022-04-25 13:25:39
...

Thinkphp join 连接查询,thinkphpjoin

 1 public function test ( )
 2         {
 3             $User = M('authlist');
 4             $rs = $User->join('left join wifi_shop  on wifi_authlist.shopid = wifi_shop.id')->field('
 5             wifi_authlist.id,
 6             wifi_authlist.shopid,
 7             wifi_authlist.routeid,
 8             wifi_authlist.mac,
 9             wifi_authlist.over_time,
10             wifi_shop.id,
11             wifi_shop.shopname,
12             wifi_shop.pid,
13             wifi_shop.trade,
14             wifi_shop.province,
15             wifi_shop.city,
16             wifi_shop.area,
17             count(wifi_authlist.mac) as times 
18             ')->where('wifi_shop.pid = %s',3)->group('wifi_authlist.mac')->order('over_time')->limit($Page->firstRow.','.$Page->listRows)->select();
19             dump($rs);
20         }

我这里查询的是 wifi_shop 跟 wifi_authlist 表,连接条件是

wifi_authlist.shopid = wifi_shop.id

一般要注意的是数据表包含前缀, 所以再join里 要加上前缀

三个表连接再加一个join就行

 1 $rs = $User->join('left join wifi_shop  on wifi_authlist.shopid = wifi_shop.id')->join('left join wifi_phonelist on wifi_authlist.mac = wifi_phonelist.mac')->field('
 2             wifi_authlist.id,
 3             wifi_authlist.shopid,
 4             wifi_authlist.routeid,
 5             wifi_authlist.mac,
 6             wifi_authlist.over_time,
 7             wifi_authlist.agent,
 8             wifi_shop.id,
 9             wifi_shop.shopname,
10             wifi_shop.pid,
11             wifi_shop.trade,
12             wifi_shop.province,
13             wifi_shop.city,
14             wifi_shop.area,
15             wifi_phonelist.mac,
16             wifi_phonelist.phone,
17             count(wifi_authlist.mac) as times 
18             ')->where('wifi_shop.pid = %s',$pid)->group('wifi_authlist.mac')->order('over_time')->limit($Page->firstRow.','.$Page->listRows)->select();
相关标签: sql语句