【ThinkPHP框架3.2版本学习总结】六、多表连接

1. 多表查询

解决方案一:

通过query方法或execute方法实现原生sql语句操作

  • query主要用于查询操作,返回数组类型数据
  • execute主要用于增、删、改操作,无论成功与否,不返回任何信息
$sql = "SELECT * FROM tp_goods g,tp_goods_class c WHERE g.gc_id = c.gc_id";
$res = M()->query($sql);
var_dump($res);

解决方案二:

使用连贯操作中table()方法实现多表链接查询

$res = M()->table('tp_goods g,tp_goods_class c')->where('g.gc_id = c.gc_id')->select();
var_dump($res);

2. 外链接查询(自连接操作)

$res = M('goods')->join('g left join tp_goods_class c on g.gc_id = c.gc_id')->select();
var_dump($res);
posted @ 2016-07-11 09:56  chenjiacheng  阅读(412)  评论(0编辑  收藏  举报