thinkphp5实现多表链接查询
这样能查询两表的所有数据
Db::table('bqg_novel')
->alias('a')//重命名
->join('bqg_chapter c','a.novelid = c.chapternovelid')
->group('chapternovelid')//去重
->select();
加field查询需要的字段
Db::table('bqg_novel')
->alias('a')
->join('bqg_chapter c','a.novelid = c.chapternovelid')
->field('a.novel_name,a.novelid,c.chapter_name')
->group('chapternovelid')
->select();
下面这种方法也能查询,不知道为啥不能去重,就用的上面的方法
Db::field('bqg_novel.novel_name,bqg_novel.novelid,bqg_chapter.chapter_name,bqg_chapter.insert_time,bqg_chapter.chapterid')
->table('bqg_novel novelid,bqg_chapter chapternovelid')
->limit(30)->select();
下面这个也能查询,原生语句,稍微麻烦一些,用框架查询还是用上面第一个方法比较好
Db::query('SELECT bqg_chapter.*
from
(
SELECT
bqg_chapter.chapter_name,
bqg_chapter.novelid,
bqg_chapte