gpt4 book ai didi

php - 使用具有相同列名的 3 个表排序

转载 作者:行者123 更新时间:2023-11-29 02:38:56 25 4
gpt4 key购买 nike

我想选择我的 3 个表并按相同列名排序

例如:

表一:id、name

表 2:id、table_1_id、进程、日期时间

表 3:id、table_1_id、进程、日期时间

我想用表 1 的 ID 选择表 2 和表 3,然后按日期时间对它们进行排序。我怎样才能在 laravel-eloquent 中做到这一点?谢谢

我想要发生的事情的例子。

id|name |process|datetime
2 |迪娜 |制作 |2019-7-15
3 |mark |成型|2019-7-14
1 |ares |折叠|2019-7-13

最佳答案

我用 union() 解决了这个问题,这里是解决方案

$table2 = DB::table('table1')
->join('table2', 'table2.table1_id', '=', 'table1.id')
->orderBy('table2.datetime', 'DESC')
->select('table1.*', 'table2.*');

$table = DB::table('table1')
->join('table3', 'table3.table1_id', '=', 'table1.id')
->orderBy('table3.datetime', 'DESC')
->select('table1.*', 'table3.*')
->union($table2)->get();

谢谢大家的帮助!

关于php - 使用具有相同列名的 3 个表排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57032775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com