Thinkphp join 连接查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thinkphp join 连接查询相关的知识,希望对你有一定的参考价值。

 1 public function test ( )
 2         {
 3             $User = M(‘authlist‘);
 4             $rs = $User->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(‘wifi_shop  on wifi_authlist.shopid = wifi_shop.id‘)->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();

 

以上是关于Thinkphp join 连接查询的主要内容,如果未能解决你的问题,请参考以下文章

thinkphp3.2 子查询

ThinkPHP数据多条件查询代码问题?

thinkphp中join用法

tp5 thinkphp5 多表关联查询 join查询

thinkphp3.2使用join联合查询

ThinkPHP join() table()方法的使用,多表查询