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.
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
This question already has answers here:
Can you use an alias in the WHERE clause in mysql?
(5 answers)
Closed 4 years ago.
I have a query that runs fine, when I try to add one more column to where clouses it cannot find the column and gives an error.
SELECT '1' AS `row_count`, (
SELECT
COUNT(*)
FROM
`attendances`
WHERE `program_sessions`.`id` = `attendances`.`program_session_id`
AND `attendances`.`deleted_at` IS NULL
) AS `attendances_count`
FROM
`program_sessions`
LEFT JOIN `programs` ON `programs`.`id` = `program_sessions`.`program_id`
LEFT JOIN `program_categories` ON `program_categories`.`id` = `programs`.`program_category_id`
LEFT JOIN `service_areas` ON `service_areas`.`id` = `program_categories`.`service_area_id`
LEFT JOIN `locations` ON `locations`.`id` = `programs`.`location_id`
WHERE (
LOWER(`program_categories`.`name`) LIKE "%3%" OR
LOWER(`programs`.`name`) LIKE "%3%" OR
LOWER(`locations`.`name`) LIKE "%3%" OR
(attendances_count = 3) OR
LOWER(`service_areas`.`name`) LIKE "%3%"
)
AND `program_sessions`.`deleted_at` IS NULL
MySQL said:
#1054 - Unknown column 'attendances_count' in 'where clause'
The query somehow cannot reach the attendances_count. What is it that I am doing wrong?
Found the problem here, aparently where clouse is not able to see the aliased columns. I should use having instead.
Can you use an alias in the WHERE clause in mysql?
My sql query shows correct in phpmyadmin but when I use it in laravel 5.4 project it shows error
SQLSTATE[42000]: Syntax error or access violation: 1055 'project_management.addprojects.id' isn't in GROUP BY (SQL: SELECT DISTINCT addprojects.id,addprojects.emp_id,sum(tasks.task_weight),tasks.flag from addprojects JOIN tasks ON tasks.proj_id = addprojects.id GROUP BY tasks.proj_id )
DB::select(" SELECT DISTINCT addprojects.proj_name,addprojects.id,
addprojects.emp_id,sum(tasks.task_weight),tasks.flag from addprojects
JOIN tasks ON tasks.proj_id = addprojects.id GROUP BY tasks.proj_id ");
It is because Laravel uses strict mode. You can disable it from config/database.php you will see in mysql array config
'strict' => true,
turn it to false and your query will work
study more about Strict SQL Mode from here
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
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?