【发布时间】:2013-10-23 20:04:14
【问题描述】:
谁能帮我解决这个问题。
表用户
+------+---------------------+
| id | name |
+------+---------------------+
| 1 | John |
| 2 | Jade |
| 3 | Robbert |
| 4 | Steve |
+------+---------------------+
表友谊
+------+---------------------+
| uid | friend_id |
+------+---------------------+
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
+------+---------------------+
- 假设当前用户 ID 为 1。
- 想获取当前用户好友的姓名。(全部)
- 但此代码只返回它找到的每个朋友的当前用户名。
对于上述示例数据,输出为:John,John 每行各一个。
$friends = DB::table('users')
->join('friendship', function($join)
{
$join->on('users.id', '=', 'friendship.uid');
})
->where('friendship.blocked', '=', '0' )
->where('users.id', '=', '1' )
->get();
SQL 代码以上:
select * from `users`
inner join `friendship`
on `users`.`id` = `friendship`.`uid`
where `users`.`id` = 1
【问题讨论】:
标签: mysql sql laravel laravel-4