"Syntax error or access violation" 多次尝试加入 table 列时出错
"Syntax error or access violation" error while trying to join a table column multiple times
我想加入名为 members
、students
和 baseinfos
的 3 个表。
并且 baseinfos 将一些 id
保存为 bas_id
并且它的名称存储在 bas_value
:
这是我的代码:
$records = DB::table('members')
->where('mys_olp_id',4)
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->join('baseinfos as gender', 'members.mbr_gender_id', '=', 'gender.bas_id as gvalue')
->join('baseinfos as degree', 'students.std_degree_id', '=', 'degree.bas_id as dvalue')
->select('gender.gvalue', 'degree.dvalue')
->get()->toArray();
但这是错误的,并向我显示了这个错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as `gvalue` inner join `baseinfos` as `degree`
请注意,我不能这样说:
$records = DB::table('members')
->where('my_students.mys_olp_id',4)
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->join('baseinfos as degree', 'students.std_degree_id', '=', 'degree.bas_id')
->join('baseinfos as gender', 'members.mbr_gender_id', '=', 'gender.bas_id')
->select('gender.bas_value', 'degree.bas_value')
->get()->toArray();
因为degree.bas_value
会覆盖gender.bas_value
!
那么如何正确连接这 3 个表?
您在 join 中的别名格式不正确。我认为你可以通过这种方式实现这一目标-
$records = DB::table('members')
->join('students', 'students.std_mbr_id', '=', 'members.mbr_usr_id')
->join('baseinfos as gender','gender.bas_id', '=', 'members.mbr_gender_id')
->join('baseinfos as degree','degree.bas_id' , '=', 'students.std_degree_id')
->where('mys_olp_id',4)
->select('gender.bas_id as gvalue', 'degree.bas_id as dvalue')
->get();
我想加入名为 members
、students
和 baseinfos
的 3 个表。
并且 baseinfos 将一些 id
保存为 bas_id
并且它的名称存储在 bas_value
:
这是我的代码:
$records = DB::table('members')
->where('mys_olp_id',4)
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->join('baseinfos as gender', 'members.mbr_gender_id', '=', 'gender.bas_id as gvalue')
->join('baseinfos as degree', 'students.std_degree_id', '=', 'degree.bas_id as dvalue')
->select('gender.gvalue', 'degree.dvalue')
->get()->toArray();
但这是错误的,并向我显示了这个错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as `gvalue` inner join `baseinfos` as `degree`
请注意,我不能这样说:
$records = DB::table('members')
->where('my_students.mys_olp_id',4)
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->join('baseinfos as degree', 'students.std_degree_id', '=', 'degree.bas_id')
->join('baseinfos as gender', 'members.mbr_gender_id', '=', 'gender.bas_id')
->select('gender.bas_value', 'degree.bas_value')
->get()->toArray();
因为degree.bas_value
会覆盖gender.bas_value
!
那么如何正确连接这 3 个表?
您在 join 中的别名格式不正确。我认为你可以通过这种方式实现这一目标-
$records = DB::table('members')
->join('students', 'students.std_mbr_id', '=', 'members.mbr_usr_id')
->join('baseinfos as gender','gender.bas_id', '=', 'members.mbr_gender_id')
->join('baseinfos as degree','degree.bas_id' , '=', 'students.std_degree_id')
->where('mys_olp_id',4)
->select('gender.bas_id as gvalue', 'degree.bas_id as dvalue')
->get();