方法一:
如果需要对多表进行操作,可以这样使用:
Db::field('user.name,role.title')
->table('think_user user,think_role role')
->limit(10)->select();
为了尽量避免和mysql的关键字冲突,可以建议使用数组方式定义,例如:
Db::field('user.name,role.title')
->table(['think_user'=>'user','think_role'=>'role'])
->limit(10)->select();
使用数组方式定义的优势是可以避免因为表名和关键字冲突而出错的情况。
方法二:
Db::table('think_artist')
->alias('a')
->join('think_work w','a.id = w.artist_id')
->join('think_card c','a.card_id = c.id')
->select();
Db::table('think_artist')
->alias('a')
->join('__WORK__ w','a.id = w.artist_id')
->join('__CARD__ c','a.card_id = c.id')
->select();
$join = [
['think_work w','a.id=w.artist_id'],
['think_card c','a.card_id=c.id'],
];
Db::table('think_user')->alias('a')->join($join)->select();
以上三种写法的效果一样,__WORK__
和 __CARD__
在最终解析的时候会转换为 think_work
和 think_card
。注意:'_表名_'这种方式中间的表名需要用大写
ps: 可以这么添加限制条件 ->where('table_a.meeting_id=table_b.meeting_id')