[PHP] tp多表查询 →→→→→进入此内容的聊天室

来自 , 2020-02-20, 写在 PHP, 查看 106 次.
URL http://www.code666.cn/view/62dad6e2
  1. 1、原生查询示例:
  2. 1       $Model = new Model();
  3. 2       $sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;
  4. 3       $voList = $Model->query($sql);
  5. 2join()方法示例:
  6. 1       $user = new Model('user');
  7. 2       $list = $user
  8.          ->join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' ); //默认左连接
  9.          ->join();//多个
  10. 3、table()方法示例:
  11.         table('表名'=>'别名')
  12.         $data=M()->table(array('user_status'=>status,'user_profile'=>profile))->where('stats.id = profile.typeid')->field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')->order('stats.id desc' )->select();;
  13.         $list = $user->table('user_status stats, user_profile profile')->where('stats.id = profile.typeid')->field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')->order('stats.id desc' )->select();
  14.  
  15. //多表查询:union查询 union('string array',true/flase);
  16. $data=M('User')->field('user_name,id')->union('select user_name,id from mk_user2')->select();//字段顺序一定要一样
  17. $data=M('User')->field('user_name,id')
  18. ->union(array('field'=>user_name,'field'=>id,'table'=>'mk_user2'),true)
  19. ->union(array('field'=>user_name,'field'=>id,'table'=>'mk_user3'),true)
  20. ->select();
  21.  
  22. //过滤查询distinct
  23. $data=M('User)->distinct(true)->field('score')->order('score asc')->select();

回复 "tp多表查询"

这儿你可以回复上面这条便签

captcha