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,
Related
I want to convert the following SQL to Laravel.
SELECT COUNT('uc.*') AS count_down, bs.brand_name
FROM `user_activities` AS uc
JOIN brands AS bs ON uc.brand_id = bs.id
GROUP BY uc.brand_id
ORDER BY count_down DESC
LIMIT 5
But when doing this:
$top_donwload_list = DB::table('user_activities')
->leftJoin('brands', 'brands.id', '=', 'user_activities.brand_id')
->selectRaw('brands.*, brands.brand_name, brands.id, count(user_activities.action_type) as user_activitiesCount')
->groupBy('user_activities.brand_id')
->get();
I get this error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'colorworld.brands.id' isn't in GROUP BY (SQL: select brands.*, brands.brand_name, brands.id, count(user_activities.action_type) as user_activitiesCount from user_activities left join brands on brands.id = user_activities.brand_id group by user_activities.brand_id)
I tried to set 'strict' => true, in database.php but I get the same error in Laravel 5.7.
Update:- database table
If I understood your question correctly, you are trying to find the count of user activities grouped by a brand's name. Also you want the top 5 records ordered by the ones with the most user activities.
So, the following should work:
$top_donwload_list = DB::table('user_activities')
->selectRaw("brands.brand_name, COUNT('user_activities.*') as user_activitiesCount")
->join('brands', 'brands.id', '=', 'user_activities.brand_id')
->groupBy('brands.brand_name')
->orderBy('user_activitiesCount', 'desc')
->take(5)
->get();
You can use Laravel Eloquent. I Assume that the relationship between brand and user_activities is one to many and Here, the "Brand" is the model of the brand entity.
$count = Brand::leftJoin
('user_activities', function($join_brands){
$join_brands-> on
('brands.id', '=','user_activity.brand_id');
)->orderBy('user_activities.count_down','desc ')
->groupBy('user_activities.brand_id')->count();
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);
Introduction
So the idea is that I have a players table, all we need to know about this table is that it has an id and username fields.
I also have a financials table, all we need to know about this table is that it references the players table through the player_id foreign key. It also has another two fields, staked and won.
A player can have many financials. So for example, a player could have 5 financials records associated with themselves.
The issue
I'm attempting to return a list of players which are sorted by their financial records. So for example, I'd like to retrieve the players who have made the most net revenue for the company. Here is a query I have written against Laravel's
Query Builder (Illuminate\Support\Facades\DB).
DB::table('financials AS f')
->select(['*', DB::raw('SUM(staked - (won)) AS net_revenue')])
->join('players AS p', function ($join) use ($param) {
$join->on('f.player_id', '=', 'p.id')
->where('p.id', 'like', '%' . $param . '%')
->orWhere('p.username', 'like', '%' . $param . '%');
})
->orderBy('net_revenue')
->groupBy(['f.id'])
->paginate($perPage);
The Error
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #9 of
SELECT list is not in GROUP BY clause and contains nonaggregated column
'master.p.id' which is not functionally dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by (SQL: select
*, SUM(staked - (won)) AS net_revenue from `financials` as `f` inner join
`players` as `p` on `f`.`player_id` = `p`.`id` and `p`.`id` like %% or
`p`.`username` like %% group by `f`.`id` order by `net_revenue` asc limit 15
offset 0)
Could someone please point me in the right direction to fixing this issue? I'd greatly appreciate it.
EDIT (08/06/2017):
$query = DB::table('financials AS f1')
->select(['f1.id', 'f1.plays', 'f1.game_id', 'f1.staked', 'f1.won', 'f1.player_id', 'f1.date', 'f1.type', DB::raw('SUM(f1.staked - (f1.won)) AS net_revenue')])
->leftJoin('financials AS f2', 'f1.player_id', '=', 'f2.player_id')
->groupBy(['f1.id', 'f1.player_id', 'f1.game_id'])
->orderBy($orderBy)
->paginate($perPage);
So my query now looks like this, and it works, it generates the net_revenue for each record. However it returns me multiple records for the same player_id.
To elaborate, if I have two records for the same player, it calculates the net_revenue for both records and orders them from highest to lowest, however what I want is only a single record for the player containing the calculation of all net_revenue.
Change the order by as SUM(staked - (won)) instead of the field name.
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
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 ...