Column Ambiguous Error in Laravel 4 - php

I have a query below in Laravel 4. in lists, I did specified 'units.unit' but I got column ambiguous error.
$unit = Unit::join('bookings','bookings.unit','=','units.unit')->where('bookings.id', '=', $id)->lists('units.unit', 'units.id');
SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'unit' dans field list est ambigu (SQL: select unit, units.id from units inner join bookings on bookings.unit = units.unit where bookings.id = 22)
Where goes wrong?

You may try this:
$unit = Unit::join('bookings', 'bookings.unit', '=', 'units.unit')
->where('bookings.id', $id)
->select(
'units.id',
'units.unit',
'bookings.id as bid',
'bookings.unit as bunit'
)
->lists('units.unit', 'units.id');

Apparently, there is bug in that library. It seems that it dropped the table prefix 'unit' while translating your query to native sql.
select unit, ... //ambiguity is between units.unit and bookings.unit columns
one would expect it to generate a statement as follows:
select units.unit, units.id from ...

Related

Integrity constraint violation: 1052

Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from kumeti_member_registrations left join members list on members list.id = kumeti_member registrations.member_name where id = 2)",
this Query please solve the error
$result = DB::table('kumeti_member_registrations')
->leftjoin('members_list', 'members_list.id', '=', 'kumeti_member_registrations.member_name')
->where('id', $id)
->get();
You need to specify the table name as id is common in both tables and is therefore ambiguous
So
->where('kumeti_member_registrations.id', $id)
Try this:
$result = DB::table('kumeti_member_registrations')
->leftjoin('members_list', 'members_list.id', '=', 'kumeti_member_registrations.member_name')
->where('kumeti_member_registrations.id', $id)
->get();

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 5.5

I just don't know what's wrong with my code and why it produces this error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous (SQL: select * from processes inner join bags on processes.bag_id = bags.id where bags.type = Recyclable and date(processes.created_at) = 2018-09-18 00:00:00 order by created_at desc limit 1) and here's my code
$bag = Bagcollect::join('bags', 'bagcollects.bag_id', '=', 'bags.id')
->select('bags.type')
->where('bagcollects.bag_id', $request->input('bag_id'))
->first();
//this query produce error
$processexist = Process::join('bags', 'processes.bag_id', '=', 'bags.id')
->where('bags.type', $bag->type)
->whereDate('processes.created_at', Carbon::today())
->latest()
->first();
You'll need to specify, in latest() the full column. latest('process.created_at') or instead of using latest() use a custom orderBy.
Thats because you are querying the 'created_at' column from two tables. You have to specify wich colums you need, for example:
$processexist = Process::join('bags', 'processes.bag_id', '=', 'bags.id')
->select('bags.column1', 'bags.columns2')
->where('bags.type', $bag->type)
->whereDate('processes.created_at', Carbon::today())
->latest()
->first();

Sql Join Query Error. Some one please assist me

This is my model function in laravel blade. In this i have two table "admins" and "candidates" using join query i join the two table. But While running my code I probably get this below mentioned
error.
if ( Input::has('recordcreator') and $request->input('recordcreator') != NULL){
$querys->join('admins','admins.adminid','=','candidates.createdby');
$querys->where('admins.firstname', 'LIKE', '%'. $request->input('recordcreator') .'%')->orWhere('admins.lastname', 'LIKE', '%'. $request->input('recordcreator') .'%')->where('admins.status','1');
}
SQLSTATE[23000]: Integrity constraint violation: 1052 Column
'firstname' in order clause is ambiguous (SQL: select * from
rsi_candidates inner join rsi_admins on rsi_admins.adminid =
rsi_candidates.createdby where rsi_admins.lastname LIKE %ic%
and rsi_admins.status = 1 or rsi_admins.firstname LIKE %ic%
group by rsi_candidates.candidateid order by firstname asc limit
20 offset 0)
As i see your laravel error you may add the table name in front of your firstname column in orderby clause
Or you may also try this solution
open your database.php file located in config directory
Config->database.php in your mysql database array add this.
'strict' => false,

Join three tables and orderBy in laravel getting an error

I'm trying to use join() in laravel5.1 but I'm getting this error:
QueryException in Connection.php line 666: SQLSTATE[23000]: Integrity
constraint violation: 1052 Column 'id' in where clause is ambiguous
(SQL: select * from quizzes inner join question_numbers on
question_numbers.quiz_id = quizzes.id and
question_numbers.question_id = multiple_choice.id inner join
multiple_choice on multiple_choice.id =
question_numbers.question_id where id = 1)
I have three tables here and I want to join these three tables. What I want first is to get a quiz where id = to a specific id then join the table question_numbers where question_numbers.quiz_id is = to the quiz.id then join the table multiple_choice where multiple_choice.id is = question_numbers.question_id and where question_numbers.question_type = multiple_choice.
What I have:
$quiz = DB::table('quizzes')->where('id', $id)
->join('question_numbers', function($join){
$join->on('question_numbers.quiz_id', '=', 'quizzes.id')
->on('question_numbers.question_id', '=', 'multiple_choice.id');
})
->join('multiple_choice', 'multiple_choice.id', '=', 'question_numbers.question_id')
->get();
dd($quiz);
First, you need to set a table aliaslike this quizzes as t for your table that belongs the where condition's id (I assume you meant quizzes table id column here) and then if you refer it inside where like where('t.id', $id), it will not complain about the ambiguous integrity because other table also have id column that's why it is showing you the below Query exception,
Column 'id' in where clause is ambiguous
Try like this,
$quiz = DB::table('quizzes as t')->where('t.id', $id)
->join('question_numbers', function($join){
$join->on('question_numbers.quiz_id', '=', 't.id');
})
->join('multiple_choice', 'multiple_choice.id', '=', 'question_numbers.question_id')
->get();
dd($quiz);

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)
->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?
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'])
->whereNotIn('employee.employee_id', ['MMMFLB003', 'guest_01', 'guest_02'])
when using join , these errors are expected if you have two fields have the same name in the tables you join between, so always try to fetch them like this
table_name.field_name

Categories