您好,我正在尝试将此SQL代码复制到Laravel qBuilder中,但没有在集合中得到想要的结果:
select chars.name, class.name, specs.name
FROM characters as chars
JOIN charclasses as class
JOIN charspecs as specs
WHERE chars.class_id = class.id
AND chars.spec_id = specs.id
然后我在Laravel中尝试了此操作
$charData = DB::table('characters')
->select('characters.*', 'charclasses.name', 'charspecs.name')
->join('charclasses', 'characters.class_id', '=', 'charclasses.id')
->join('charspecs', 'characters.spec_id', '=', 'charspecs.id')
->orderBy('user_id', 'ASC')
->get();
dd($charData);
,结果是:
答案 0 :(得分:1)
问题是您要选择三个具有相同名称的字段。这些字段在php代码中添加到了一个数组中,但是该数组字段被覆盖了两次,因为关联数组中没有重复的键。如果要选择三个名称,则必须给它们起另一个名称。
$charData = DB::table('characters')
->select('characters.*', 'charclasses.name as charclassname', 'charspecs.name as charspecname')
->join('charclasses', 'characters.class_id', '=', 'charclasses.id')
->join('charspecs', 'characters.spec_id', '=', 'charspecs.id')
->orderBy('user_id', 'ASC')
->get();
dd($charData);