mysql-根据Laravel中的多个表排序

mysql-根据Laravel中的多个表排序,mysql,laravel,sql-order-by,Mysql,Laravel,Sql Order By,我有三张桌子,第一张,第二张,第三张。由于属性上的巨大差异,我分成了3个表。所有这些表都有一个公共列created_timestamp 我想创建一个时间线,显示最新的时间,并以创建的时间戳作为参考 我曾尝试按最大值(t1.created_timestamp,t2.created_timestamp,t3.created_timestamp)连接表和顺序,但在laravel的查询生成器中无法做到这一点 另外,当显示属性时,它们将重复,如果topic1是最新的,是否有方法仅显示topic1属性,依此

我有三张桌子,第一张,第二张,第三张。由于属性上的巨大差异,我分成了3个表。所有这些表都有一个公共列created_timestamp

我想创建一个时间线,显示最新的时间,并以创建的时间戳作为参考

我曾尝试按最大值(t1.created_timestamp,t2.created_timestamp,t3.created_timestamp)连接表和顺序,但在laravel的查询生成器中无法做到这一点

另外,当显示属性时,它们将重复,如果topic1是最新的,是否有方法仅显示topic1属性,依此类推

还是有更好的方法

谢谢绝地大师们

我忘了提到第四个表(follower表),它只显示3个主题表中创建的表

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\u id=user\u id、teacher\u id、visitor\u id(表连接)
我只需要获取我所跟随的行(aka where follower.creator\u 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');

我认为在我的情况下这样做有点困难。我想试试你的答案(看起来很完美),但我还有一个问题忘了指出。可怕的。第四个表(follower)仅显示所有3个主题的where创建。当我将表拆分时,有没有办法做到这一点?是否要将为关注者表中的每个主题创建的\u by包含到最终集合中?下表具有一个follow\u id,该id连接topic1.user\u id、topic2.teacher\u id和topic3.visitor\u id。我将在user\u id上加入以下内容,教师id和访客id以及where follow\u id=$variable\u follow我已经更新了我的答案,以便它每次都加入followers表,并确保follower id是您。我不知道为什么这是必要的,因为你可以确保
主题1.用户id
主题2.教师id
,和
主题3.访客id
与你的id匹配。谢谢!我马上去看看。topic1.user\u id、topic2.teacher\u id和topic3.visitor\u id只是加入因素。我希望这3个表的行基于follower.creator\u id,即我。下面的id是与其他3个表联接以获取它们的行。