laravel DB 连接查询取别名出错了,table不存在

WBOY
发布: 2016-08-04 09:21:19
原创
1348 人浏览过

<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>
登录后复制
登录后复制

连接查询取别名出错了
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)

回复内容:

<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>
登录后复制
登录后复制

连接查询取别名出错了
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)

最好的做法是把这SQL复制到数据库管理工具中执行下,看是否是你构造的有问题

从你构造的sql看,是因为别名中加上了表前缀'stb'

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板