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