doukang7501 2015-12-09 15:10
浏览 25
已采纳

使用'whereNotIn'的查询生成器抛出错误

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)

->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)

any ideas, help please?

  • 写回答

2条回答 默认 最新

  • douyuepi6485 2015-12-09 15:20
    关注

    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. and employee.

    Now on your whereNotIn you also have to prefix it, so the query engine knows which table column you are trying to reference. So you only have to add the prefix in your whereNotIn clause:

    ->whereNotIn('employee.employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?