You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中实现多表合并?附具体示例需求

嘿,这事儿我熟!在Laravel里合并多张数据表,主要看你的表结构和需求——是同结构表去重/不去重合并,还是不同结构表对齐后合并,我给你一步步讲清楚,再上示例!

1. 同结构表的合并(Union/Union All)

如果你的两张表字段完全一致(字段数、字段类型都匹配),那直接用union或者unionAll就搞定了:

  • union:会自动去除合并结果里的重复数据
  • unionAll:保留所有数据(包括重复项),性能比union更高

示例场景

假设你有两张表:registered_users和guest_users,结构都是id, name, email, created_at,要合并成一个完整的用户列表。

代码实现

// 先构造第一个表的查询
$registeredUsers = DB::table('registered_users')
    ->select('id', 'name', 'email', 'created_at');

// 构造第二个表的查询,字段要和第一个完全对应
$guestUsers = DB::table('guest_users')
    ->select('id', 'name', 'email', 'created_at');

// 执行合并(选union或unionAll)
$mergedUsers = $registeredUsers->union($guestUsers)->get();
// 或者保留重复数据:$registeredUsers->unionAll($guestUsers)->get();
2. 不同结构表的对齐合并

如果两张表结构不一样,但你想提取共同需要的字段合并成统一结构,那就用select给字段起别名,对齐字段名和类型。

示例场景

比如有posts表(字段:id, title, content, created_at)和articles表(字段:id, title, body, published_at),要合并成一个包含「ID、标题、内容、发布时间」的内容列表。

代码实现

// 处理posts表,把content别名成body,created_at保留
$posts = DB::table('posts')
    ->select('id', 'title', 'content as body', 'created_at');

// 处理articles表,把published_at别名成created_at,body直接用
$articles = DB::table('articles')
    ->select('id', 'title', 'body', 'published_at as created_at');

// 合并后获取结果
$mergedContent = $posts->union($articles)->orderBy('created_at', 'desc')->get();
3. Eloquent模型层面的合并

如果你用的是Eloquent模型(比如Post和Article模型),同样可以用union,写法更贴合Laravel的ORM风格:

// 从模型构造查询,对齐字段
$posts = Post::select('id', 'title', 'content as body', 'created_at');
$articles = Article::select('id', 'title', 'body', 'published_at as created_at');

// 合并并排序
$mergedItems = $posts->union($articles)->orderBy('created_at', 'desc')->paginate(10);
关键注意事项
  • 用union/unionAll时,两个查询的字段数量、对应字段的类型必须一致,否则会报错
  • 合并后的查询支持链式调用orderBy、where、paginate等方法,和普通查询一样用
  • 如果需要合并3张及以上的表,只需要连续调用union/unionAll就行,比如$query1->union($query2)->union($query3)

内容的提问来源于stack exchange,提问作者ufuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:02:57