我有 3 个表 topic1、topic2、topic3。由于属性差异巨大,我分成了3个表。所有这些表都有一个公共(public)列created_timestamp
我想创建一个从最新的时间线开始显示的时间线,并以created_timestamp作为引用。
我尝试过连接表并按最大顺序排序(t1.created_timestamp、t2.created_timestamp、t3.created_timestamp),但我无法在 laravel 的查询生成器中执行此操作。
另外显示属性时,会重复显示,有没有办法只显示topic1属性,如果topic1是最新的,依此类推。
或者有更好的方法吗?
谢谢绝地大师!
我忘了提及第四个表(follower 表),它仅显示 3 个主题表的created_by。
topic1 {var1, var2, created_timestamp, user_id)
topic2 {var3, var2, var3, created_timestamp, teacher_id)
topic3 {var3, created_timestamp, visitor_id)
follower {follow_id, creator_id}
follow_id = user_id、teacher_id、visitor_id(表连接) 我只需要获取我关注的行(又名 where follower.creator_id = $me)
最佳答案
我认为最好的方法是对每个表运行查询,然后合并结果。
$a = DB::table('topic1')->join('follower', 'folower.follow_id', '=', 'topic1.user_id')->where('folower.follower_id', $followed)->get();
$b = DB::table('topic2')->join('follower', 'folower.follow_id', '=', 'topic2.teacher_id')->where('folower.follower_id', $followed)->get();
$c = DB::table('topic3')->join('follower', 'folower.follow_id', '=', 'topic3.visitor_id')->where('folower.follower_id', $followed)->get();
$results = new Illuminate\Database\Eloquent\Collection;
$results = $results->merge($a)->merge($b)->merge($c)->sortBy('created_timestamp');
关于mysql - 从 Laravel 中的多个表中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29974432/