我有2张桌子
+----+------------+------------+
| id | first_name | date |
+----+------------+------------+
| 1 | Bob | 01/01/2019 |
+----+------------+------------+
| 2 | June | 01/05/2019 |
+----+------------+------------+
和
+----+--------+---------+------------+
| id | userID | comment | date |
+----+--------+---------+------------+
| 1 | 1 | Hello | 02/01/2019 |
+----+--------+---------+------------+
| 2 | 1 | Again | 02/02/2019 |
+----+--------+---------+------------+
| 3 | 2 | Howdy | 02/03/2019 |
+----+--------+---------+------------+
我想获得所有名字为 Bob 的用户的结果,并将最新的评论附加到该结果。
我可以
$users = SELECT * FROM Users WHERE id = 1
然后
foreach($users as $user){
$comment = Comment::where("userID", $user->id)->first();
if($comment != null){
$user->comments = $comment->$comment;
}
}
但是,如果 $users 中有很多结果,它会很慢
开满天机
德玛西亚99