简体   繁体   English

Laravel-语法错误或访问冲突:1066不是唯一的表/别名:

[英]Laravel - Syntax error or access violation: 1066 Not unique table/alias:

I got this error message when trying to join multiple tables 尝试连接多个表时收到此错误消息

Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users' (SQL: select * from `prescriptions` inner join `doctors` on `prescriptions`.`doctor_id` = `doctors`.`id` inner join `users` on `doctors`.`user_id` = `users`.`id` inner join `patients` on `prescriptions`.`patient_id` = `patients`.`id` inner join `users` on `patients`.`user_id` = `users`.`id` inner join `pharmacies` on `prescriptions`.`patient_id` = `pharmacies`.`id` inner join `users` on `pharmacies`.`user_id` = `users`.`id` where `prescriptions`.`pharmacy_id` = 1)

My code is like this 我的代码是这样的

$prescriptions = DB::table('prescriptions') 
        ->join('doctors', 'prescriptions.doctor_id', '=', 'doctors.id')
        ->join('users', 'doctors.user_id', '=', 'users.id')
        ->join('patients', 'prescriptions.patient_id', '=', 'patients.id')
        ->join('users', 'patients.user_id', '=', 'users.id')
        ->join('pharmacies', 'prescriptions.patient_id', '=', 'pharmacies.id')
        ->join('users', 'pharmacies.user_id', '=', 'users.id')
        ->where('prescriptions.pharmacy_id',$pharmacy->id)
        ->get();

My ERD diagram 我的ERD图

在此处输入图片说明

As others have correctly pointed out, the problem is that you've joined in the users table three times but since they have not been aliased, your database is unable to distinguish which table you mean when you try to use it in any way. 正如其他人正确指出的那样,问题在于您已经在users表中加入了3次,但是由于没有别名,因此数据库无法区分以任何方式使用它时要使用的表。 The solution would be to alias each users table when you join them. 解决方案是在加入每个users表时为其别名。 For example: 例如:

$prescriptions = DB::table('prescriptions') 
    ->join('doctors', 'prescriptions.doctor_id', '=', 'doctors.id')
    ->join('users as du', 'doctors.user_id', '=', 'du.id')
    ->join('patients', 'prescriptions.patient_id', '=', 'patients.id')
    ->join('users as pu', 'patients.user_id', '=', 'pu.id')
    ...
    ->get([
        'du.name as doctor_name',
        'pu.name as patient_name',
        'prescriptions.description as description'
    ]);

To grab the doctor's name for example: 以医生的名字为例:

foreach ($prescriptions as $prescription) {
    echo $prescription->doctor_name;
}

However, I would suggest doing this with Eloquent instead: 但是,我建议改为使用Eloquent:

$prescriptions = Prescription::where('pharmacy_id', $pharmacyId)
    ->with('patient', 'patient.user', 'doctor', 'doctor.user');

This will effectively run one query to select all prescriptions by the given pharmacy_id and then it will run subsequent queries to eagerly load in the associated patients (and their associated users) and those doctors (and their associated users). 这将有效地运行一个查询以通过给定的pharmacy_id选择所有处方,然后它将运行后续查询以热切地加载相关的患者(及其相关用户)和那些医生(及其相关的用户)。

If you don't need to load the pharmacy name or any other information about the pharmacy or its associated user, then you needn't bother load them in. If you do, it's as simple as following the above format and adding them to the ->with() which will result in them being eager loaded. 如果您不需要加载药房名称或有关该药房或其关联用户的任何其他信息,则无需费心加载它们。如果需要,只需按照上述格式并将其添加到->with() ,这将导致它们被迫加载。

After doing the above, you can access a patient's name like this: 完成上述操作后,您可以像这样访问患者的姓名:

foreach ($prescriptions as $prescription) {
    echo $prescription->patient->user->name;
}

This of course assumes you have your relations all correctly set up. 当然,这假定您已正确建立了所有关系。

this is because you're using the same alias for 3 joins .. this will do: 这是因为您对3个连接使用了相同的别名..这样做:

$prescriptions = DB::table('prescriptions') 
    ->leftJoin('doctors', function($join){
        $join->join('users as a', 'doctors.user_id', '=', 'a.id');
    })
    ->leftJoin('patients', function($join){
        $join->join('users as b', 'patients.user_id', '=', 'b.id');
    })
    ->join('pharmacies', function($join){
        $join->join('users as c', 'pharmacies.user_id', '=', 'c.id');
    })
    ->where('prescriptions.pharmacy_id',$pharmacy->id)
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel-语法错误或访问冲突:1066不是唯一的表/别名 - Laravel - Syntax error or access violation: 1066 Not unique table/alias 语法错误或访问冲突:1066 不是唯一的表/别名:'tasks' - Syntax error or access violation: 1066 Not unique table/alias: 'tasks' SQLSTATE [42000]:语法错误或访问冲突:1066在laravel中不是唯一的表/别名 - SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias in laravel 为什么我会收到此错误? 语法错误或访问冲突:1066不唯一的表/别名:'Employees' - Why do I get this error? Syntax error or access violation: 1066 Not unique table/alias: 'Employees' Laravel:1066不是唯一的表/别名 - Laravel: 1066 Not unique table/alias Laravel - 1066 不是唯一的关系表/别名 - Laravel - 1066 Not unique table/alias on a relationship SQLSTATE [42000]:语法错误或访问冲突:1066 - SQLSTATE[42000]: Syntax error or access violation: 1066 Codeignitor 错误号:1066“不是唯一的表/别名” - Codeignitor Error Number: 1066 "Not unique table/alias" 错误:不是唯一的表/别名:'companies'数据库(错误):1066 - Error: Not unique table/alias: 'companies' Database (error): 1066 为什么错误#1066-不是唯一的表/别名:“ cat_rapoarte” - Why error #1066 - Not unique table/alias: 'cat_rapoarte'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM