laravel 这个同样两张表取别名查询出错,怎么改呢?
程序员文章站
2022-03-10 12:04:25
...
$res = DB::table('topics')->select('topics.*', 'b.username',
'b.avatar', 'c.username as rname', 'd.cname')
->where('topics.is_hidden', 0)
->leftJoin('users b', 'b.uid', '=', 'topics.uid')
->leftJoin('users c', 'c.uid', '=', 'topics.ruid')
->leftJoin('nodes d', 'd.node_id', '=', 'topics.node_id')
->orderBy('ord', 'desc')
->take($limit)->get();
这里错误很多:
Table 'startbbs.stb_users b' doesn't exist
stb_users 我要用到两次,肯定得取别名,怎么办呢
回复内容:
$res = DB::table('topics')->select('topics.*', 'b.username',
'b.avatar', 'c.username as rname', 'd.cname')
->where('topics.is_hidden', 0)
->leftJoin('users b', 'b.uid', '=', 'topics.uid')
->leftJoin('users c', 'c.uid', '=', 'topics.ruid')
->leftJoin('nodes d', 'd.node_id', '=', 'topics.node_id')
->orderBy('ord', 'desc')
->take($limit)->get();
这里错误很多:
Table 'startbbs.stb_users b' doesn't exist
stb_users 我要用到两次,肯定得取别名,怎么办呢
$res = DB::table('topics')->select('topics.*', 'b.username',
'b.avatar', 'c.username as rname', 'd.cname')
->where('topics.is_hidden', 0)
->leftJoin('users AS b', 'b.uid', '=', 'topics.uid')
->leftJoin('users AS c', 'c.uid', '=', 'topics.ruid')
->leftJoin('nodes AS d', 'd.node_id', '=', 'topics.node_id')
->orderBy('ord', 'desc')
->take($limit)->get();
上一篇: Yii CDbCriteria
下一篇: natsort()的10篇内容推荐