首頁  >  文章  >  後端開發  >  laravel 這個同樣兩張表取別名查詢出錯,要怎麼改呢?

laravel 這個同樣兩張表取別名查詢出錯,要怎麼改呢?

WBOY
WBOY原創
2016-08-04 09:21:171248瀏覽

<code>$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();
</code>

這裡錯誤很多:
Table 'startbbs.stb_users b' doesn't exist
stb_users 我要用到兩次,肯定得取別名,怎麼辦呢

回覆內容:

<code>$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();
</code>

這裡錯誤很多:
Table 'startbbs.stb_users b' doesn't exist
stb_users 我要用到兩次,肯定得取別名,怎麼辦呢

<code class="php">$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();</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn