I have been using Laravel 5 for a week and I've now come to the point where I want to convert all my existing raw SQL queries using Query Builder but I have a problem.
When I run the following query, I get this error message
SQLSTATE[42000]: Syntax error or access violation: 1055
'procurement.pp_proposals.title' isn't in GROUP BY
$proposals = DB::table('tableA')
->join('tableB', 'tableA.id', '=', 'tableB.id')
->leftJoin('tableC', 'tableA.id', '=', 'tableC.id')
->select(DB::raw('tableA.id, title, date_created, date_completed, percent_complete, complete, COALESCE(COUNT(tableC.id), 0) AS total_ids'))
->where([
['tableB.user', '=', Auth::user()->username],
['submitted', '=', '0'],
])
->groupBy('tableA.id')
->orderBy('title', 'asc')
->get();
This is my raw SQL that works perfectly fine so I don't understand why i need to GROUP BY on all the extra columns
SELECT tableA.id,
title,
date_created,
date_completed,
percent_complete,
complete,
COALESCE(COUNT(tableC.id), 0) AS 'total_ids'
FROM tableA
INNER JOIN tableB
ON tableA.id = tableB.id
LEFT JOIN tableC
ON tableA.id = tableC.id
WHERE submitted = '0' AND tableB.user = 'user'
GROUP BY tableA.id
ORDER by title ASC
I am not sure how it works but surely you can fix that issue by setting strict = false on the config/database.php file to your MySQL config!
I had the same issue and setting it to false solve my problem!
Actually I just opened a question here to find why it happens!
If you want to follow: Strict database config Laravel 5.2 to Laravel 5.3
Related
I have a code where Eloquent query builder joins the ManyToOne tables articles and users.
The code looks like:
$model = Article::with('user')->select('articles.*'));
and then the model filters the user name
$model = $model->join('users', 'articles.user_id', '=', 'users.id')
->where('users.name', 'like', "%$value%");
This throws me an error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
table/alias: 'users' (SQL: select count(*) as aggregate from
articles inner join users on articles.user_id = users.id
inner join users on articles.user_id = users.id where
users.name like %may% and users.name like %may% and
articles.deleted_at is null)
Look at the duplicate inner join on users table and also duplicate where clause.
The same code on localhost works fine and create sql:
select count(*) as aggregate from `articles` inner join `users` on `articles`.`user_id` = `users`.`id` where `users`.`name` like '%may%' and `articles`.`deleted_at` is null
Original code is here: https://github.com/camohub/laravel-datagrid-example/blob/master/app/Http/Controllers/DefaultController.php#L25
and the live error is here: https://laravel-datagrid.tatrytec.eu/?chgrid-filter-username=may&chgrid-perPage=25
I dont understand. It looks like some database setting is wrong.
Hope somebody knows what happened there. Thanks a lot.
EDIT: The issue is caused by PHP version. Production is lower 7.4.3 than localhost 7.4.19
The solution is to write an envelope above the Eloquent query builder. Here it is https://github.com/camohub/laravel-datagrid/blob/master/src/QueryBuilder.php
I have a part of the SQL-code that needs to be converted correctly for Query Builder (Laravel).
select
p.*
from posts p, comments c
where c.post_id = p.post_id
group by p.post_id
order by avg(c.mark_first) desc
Can anyone halp me? :/
Using query builder you could write it as
$posts = DB::table('posts as p')
->join('comments as c', 'p.post_id', '=', 'c.post_id')
->groupBy('p.post_id')
->orderByRaw('avg(c.mark_first) desc')
->select('p.*')
->get();
Its better if you could add all the columns in groupby which you want in select part because above query will become invalid if mysql's full_group_by mode is enabled which is enabled by default in 5.7+
This question already has answers here:
Laravel : Syntax error or access violation: 1055 Error
(15 answers)
Closed 5 years ago.
i have an issue, and don't know how fix it.
I need to get an specific object from select such as
{
"iso":"UA",
"stories":122,
"title":Ukraine
}
so i have an sql query
SELECT
c.iso,
locale.title as title,
(SELECT COUNT(DISTINCT s.id) FROM stories AS s WHERE s.country_id = c.id) AS stories
FROM `countries` AS c
LEFT JOIN countries_locale AS c_l ON c.id=c_l.country_id
LEFT JOIN locales AS locale ON c_l.locale_id=locale.id
WHERE locale.locale = 'en'
GROUP BY c.id
HAVING stories>0
and it works fine, so i try to rewrite this query to Laravel QB:
DB::table($this->getTable())
->select(
'countries.iso as iso',
'locales.title as title',
DB::raw('(SELECT COUNT(s.id) FROM stories AS s WHERE s.country_id = countries.id) AS stories')
)
->leftJoin('countries_locale', 'countries.id', '=', 'countries_locale.country_id')
->leftJoin('locales', 'countries_locale.locale_id', '=', 'locales.id')
->where('locales.locale', \App::getLocale())
->groupBy('iso')
->having('stories', '>', 0)
->get();
And then i get an error
Syntax error or access violation: 1055 'way.countries.id' isn't in GROUP BY
and in shows me an sql string, that i can successfully exec in mysql
If you want laravel to accept not very strict queries,use
'strict' => false
in the database configuration.
Or,in your case you could put both those column in the select in the group by.
I am trying to get following query on Laravel:
SELECT
table1.* ,
table1.colb as aaa
FROM
table1
LEFT jOIN table2 on table2.cola = table1.colb
This is the Laravel DB Query code :
DB::table('table1')
->leftJoin('table2','table2.cola', '=', 'table1.colb')
->select('table1.* , table1.colb as aaa')
->get();
But it doesn't work and I get SQL Syntax error.
This is the SQL which Laravel is making using above code which is wrong :
select `table1`.* as `table1.colb`
from `table1`
left join `table2` on `table2`.`cola` = `table1`.`colb`
How can I fix this using laravel way?
You need to pass multiple parameters to the select method for each one to get prepared correctly:
DB::table('TABLE1')
->leftJoin('TABLE2','TABLE2.COLA', '=', 'TABLE1.COLB')
->select('TABLE1.*', 'TABLE1.COLB AS AAA') // <- 2 separate params here
->get();
I'm having an issue getting a COUNT() from a SQL query using Zend_Db_Table_Select, and I think it may be a possible bug because the SQL it should be generating actually works. Here's the Zend Select Query: ($this is a Zend_Db_Table, renamed to table1 in this example)
$select = $this->select();
$select->setIntegrityCheck(false);
// Select Count
$select->from($this, array("COUNT(*) as 'COUNT'"))
->joinLeft('users', 'table1.userID = users.userID')
->joinLeft('table2', 'users.anotherKey = table2.anotherKey');
// Add Where clause after join
$select->where('users.anotherKey = ?', $anotherKeyValue);
This gives the error:
SQLSTATE[42000]: Syntax error or access violation: 1140
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
illegal if there is no GROUP BY clause`
However, this query...
SELECT COUNT(*) AS 'count' FROM table1
LEFT JOIN users ON table1.userID = users.userID
LEFT JOIN table2 ON users.anotherKey = table2.anotherKey
WHERE users.anotherKey = [anotherKeyValue]
...returns the expected results with no errors when run against the database. Any ideas whats going on, why the error, and how to get around it?
have you tried to see actual query, that zend_db produce?