首页 > 开发 > ThinkPHP > 正文

thinkphp多表查询语句

2020-10-12 21:05:41
字体:
来源:转载
供稿:网友

1、table()函数

thinkphp中提供了一个table() 函数,具体用法参考以下语句:

  1. $list=$Demo->table('think_blog blog,think_type type')->where("blog.typeid=type.id and blog.id='$id'")->field('blog.id as id,blog.title,blog.content,type.typename as type')->order('blog.id desc' )->limit(5)->select(); 
  2. echo $Demo->getLastSql(); //打印一下SQL语句,查看一下 

2、join()函数,看一下代码:

  1. $Demo = M('artist'); 
  2. $Demo->join('RIGHT JOIN think_work ON think_artist.id = think_work.artist_id' ); 
  3. //可以使用INNER JOIN 或者 LEFT JOIN 这里一定要注意表名的前缀! 
  4. echo $Demo->getLastSql(); //打印一下SQL语句,查看一下 

3、原生查询

  1. $Model = new Model(); 
  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; 
  3. $voList = $Model->query($sql); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选