Home  >  Article  >  Backend Development  >  Laravel DB connection query alias error, table does not exist

Laravel DB connection query alias error, table does not exist

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

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

An error occurred during the connection query aliasing
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'startbbs.stb_users b' doesn't exist (SQL: select stb_topics.*, stb_b.username, stb_b.avatar, stb_c.username as rname, stb_d.cname from stb_topics left join stb_users b on stb_b.uid = stb_topics. uid left join stb_users c on stb_c.uid = stb_topics.ruid left join stb_nodes d on stb_d.node_id = stb_topics.node_id where stb_topics .is_hidden = 0 order by ord desc limit 20)

Reply content:

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

An error occurred during the connection query aliasing
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'startbbs.stb_users b' doesn't exist (SQL: select stb_topics.*, stb_b.username, stb_b.avatar, stb_c.username as rname, stb_d.cname from stb_topics left join stb_users b on stb_b.uid = stb_topics. uid left join stb_users c on stb_c.uid = stb_topics.ruid left join stb_nodes d on stb_d.node_id = stb_topics.node_id where stb_topics .is_hidden = 0 order by ord desc limit 20)

The best way is to copy this SQL to the database management tool and execute it to see if there is something wrong with your construction

From the sql you constructed, it is because the table prefix 'stb' is added to the alias

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