程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> Thinkphp join 連接查詢,thinkphpjoin

Thinkphp join 連接查詢,thinkphpjoin

編輯:關於PHP編程

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();

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved