Home  >  Article  >  Backend Development  >  In laravel, there is an error in aliasing the same two tables. How to correct it?

In laravel, there is an error in aliasing the same two tables. How to correct it?

WBOY
WBOYOriginal
2016-08-04 09:21:171246browse

<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>

There are many errors here:
Table 'startbbs.stb_users b' doesn't exist
stb_users I want to use it twice, so I must use an alias, what should I do?

Reply content:

<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>

There are many errors here:

Table 'startbbs.stb_users b' doesn't exist
stb_users I want to use it twice, so I must use an alias, what should I do?

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn