2 回答
TA贡献1795条经验 获得超7个赞
两种情况的结果相同。他们只是使用相同的属性,因此雄辩的结果相互覆盖。使用别名来解决问题
SELECT status, contacts.name as contact_name, contacts.telephone_number, companies.name as company_name, roles.name as role_name , stages.description, actions.description FROM entries JOIN contacts ON entries.contact_id = contacts.id JOIN companies ON contacts.id = companies.contact_id JOIN roles ON companies.id = roles.company_id JOIN stages ON roles.id = stages.role_id JOIN actions ON stages.id = actions.stage_id
TA贡献1773条经验 获得超3个赞
您应该设置别名以避免覆盖。另一件事是,如果你使用 Laravel,那么 eloquent 就是你的朋友:
$entries = DB::table('entries')
->join('contacts', 'entries.contact_id', '=', 'contacts.id')
->join('companies', 'contacts.id', '=', 'companies.contact_id')
->join('roles', 'companies.id', '=', 'roles.company_id')
->join('stages', 'roles.id', '=', 'stages.role_id')
->join('actions', 'stages.id', '=', 'actions.stage_id')
->select('status', 'contacts.name AS contact_name', 'contacts.telephone_number', 'companies.name AS company_name', 'roles.name AS role_name', 'stages.description AS stage_description', 'actions.description AS action_description')
->get();
更好的方法是使用模型和集合关系。
- 2 回答
- 0 关注
- 112 浏览
添加回答
举报