Thinkphp join 连接查询,thinkphpjoin_PHP教程
程序员文章站
2024-01-07 14:16:10
...
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();
推荐阅读
-
Thinkphp join 连接查询,thinkphpjoin_PHP教程
-
ThinkPHP 连接Oracle数据库的详细教程[全]_PHP
-
php 数据库mysql查询与连接类_PHP教程
-
thinkphp模型过滤查询字段,表达式方法_PHP教程
-
将子查询改造成join连接查询
-
ThinkPHP采用原生query实现关联查询left join实例
-
PHP中实现mysql连接与查询实例详解_PHP教程
-
ThinkPHP连接数据库及主从数据库的设置教程,thinkphp主从
-
ThinkPHP整合Smarty模板引擎的使用以及查询方法汇总_PHP教程
-
ThinkPHP 连接Oracle数据库的详细教程[全]