Integrity constraint violation: 1052 - php

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

Related

Yii2 GridView: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous

could someone explain me why I'm getting this error? I've tried to search related information, but couldn't find what would be useful.
Here is my function:
$query = Recipient::find()->from('recipient')
->joinWith(['delivers'])
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
Error is:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in
order clause is ambiguous
The SQL being executed was:
SELECT COUNT(*) FROM `recipient`
LEFT JOIN `deliver` ON `recipient`.`id` = `deliver`.`recipient_id`
ORDER BY `id` DESC
Try
Recipient::find()->from('recipient')->joinWith(['delivers'])->orderBy(['`recipient`.`id`' => SORT_ASC]);
In above example, column name is explicitly specified to avoid ambiguity error.
Code seems incomplete. Post more code, so we can help you.

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

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

Laravel Join multiple table not working

I'm having an issue on Laravel 5.4 when I try to use only one join it works ok and returns correct data, but their add another join it doesn't work.
$data = Player::select(DB::raw('CONCAT(familyName,", ",firstName) AS fullName'))
->where('firstname', 'like', '%'.$search.'%')
->orWhere('familyName', 'like', '%'.$search.'%')
->orderBy('familyName', 'asc')
->join('teams', 'players.primaryClubId', '=', 'teams.clubId')
->join('person_competition_statistics', 'players.personId', '=', 'person_competition_statistics.personId')
->addSelect(['players.*', 'teams.teamName', 'teams.teamNickname', 'teams.teamCode'])
->get()
->unique() //remove duplicates
->groupBy(function($item, $key) { //group familyName that starts in same letter
return substr($item['familyName'], 0, 1);
})
->map(function ($subCollection) {
return $subCollection->chunk(4); // put your group size
});
return $data;
Returned Error:
QueryException in Connection.php line 647:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'familyName' in field list is ambiguous (SQL: select CONCAT(familyName,", ",firstName) AS fullName, `players`.*, `teams`.`teamName`, `teams`.`teamNickname`, `teams`.`teamCode` from `players` inner join `teams` on `players`.`primaryClubId` = `teams`.`clubId` inner join `person_competition_statistics` on `players`.`personId` = `person_competition_statistics`.`personId` where `firstname` like %% or `familyName` like %% order by `familyName` asc)
If you are joining table then you should give table alias. like team as t , players as p and then column name like p.playername

Column Ambiguous Error in Laravel 4

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

Categories