简体   繁体   English

使用“ whereNotIn”的查询生成器将引发错误

[英]query builder using 'whereNotIn' throws error

I'm trying to fetch records with an array of exceptions, here's what I tried (refer below) 我正在尝试使用一系列异常来获取记录,这就是我尝试过的内容(请参阅下文)

$users_nowishlist = DB::table('employee')
    ->join('users', 'users.employee_id', '=', 'employee.employee_id')
    ->where('has_wishlist', '=', "0")
    ->whereNotIn('employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])
    ->where('employment_status', '=', 'ACTIVE')
    ->get();

so in this line was my records filter, means only records that does not equal to any of those 'employee_id' from the exceptions array will be return (refer below) 因此,在这一行中是我的记录过滤器,这意味着将返回仅与exceptions数组中的那些“ employee_id”不相等的记录(请参阅下文)

->whereNotIn('employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])

but instead I got this error (refer below): 但是相反,我得到了这个错误(请参阅下文):

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'employee_id' in where clause is ambiguous (SQL: select * from employee inner join users on users . employee_id = employee . employee_id where has_wishlist = 0 and employee_id not in (MMMFLB003, guest_01, guest_02, guest_03) and employment_status = ACTIVE) SQLSTATE [23000]:完整性约束违规:1052列在where子句是不明确的'EMPLOYEE_ID'(SQL:SELECT * FROM employee内部联接usersusersemployee_id = employeeemployee_id其中has_wishlist = 0和employee_id在(MMMFLB003,guest_01没有, guest_02,guest_03)和employment_status = ACTIVE)

any ideas, help please? 有什么想法,请帮忙吗?

This happens because when you are doing the join there are two columns with the same name. 发生这种情况的原因是,当执行联接时,会有两列具有相同的名称。

That's why on your join you prefix the employee_id with users. 这就是为什么在您的加入中要在employee_id加上users. and employee. employee.

Now on your whereNotIn you also have to prefix it, so the query engine knows which table column you are trying to reference. 现在,在whereNotIn还必须为其whereNotIn前缀,以便查询引擎知道您要引用的表列。 So you only have to add the prefix in your whereNotIn clause: 因此,您只需要在whereNotIn子句中添加前缀:

->whereNotIn('employee.employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])
->whereNotIn('employee.employee_id', ['MMMFLB003', 'guest_01', 'guest_02']) 

使用join时,如果您在两个表之间有两个相同名称的字段,则会出现这些错误,因此请始终尝试按以下方式获取它们

table_name.field_name

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM