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
Related
When I upgraded my ubuntu from 15.10 to 16.04 I have this error in my yii project:
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]:
Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
'v4master.A.id' which is not functionally dependent on columns in
GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by. The SQL statement executed was:
The sql code is follows:
$sql = "SELECT A.*,B.`type_name` FROM `".$this->tbl_auth_user_items."` A
INNER JOIN `".$this->tbl_auth_context_types."` B ON A.`type_id` = B.`type_id`
WHERE A.`user_id` = '".$user_id."' GROUP BY A.`type_id`, A.`type_value` ORDER BY A.`type_id` DESC";
return $this->_db->createCommand($sql)->queryAll();
Run this query at Mysql;
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Coild that your upgrade implies also a db upgrade to mysql 5.7 if so you must know that
starting for mysql 5.7 the use of column in select without aggreation function and not mentioned in group by is not allowed by default
if you need distinct result set ypu should use distinct clause and not group by
$sql = "SELECT DISTINCT
A.*,B.`type_name`
FROM `".$this->tbl_auth_user_items."` A
INNER JOIN `".$this->tbl_auth_context_types."` B ON A.`type_id` = B.`type_id`
WHERE A.`user_id` = '".$user_id."'
ORDER BY A.`type_id` DESC";
return $this->_db->createCommand($sql)->queryAll();
or ( but is deprecated) you can change the sql_mode for reverting to the previuos version behavior
fro
sql_mode = 'ONLY_FULL_GROUP_BY';
to
SET sql_mode = ''
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 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 need to join two tables where a substring of the base table's join column's value is used to join with the other table.
I'm using zend framework 2 and I've written the query as follows.
$select = new Select($this->table);
$select->columns(array('match_code'))
->join('countries','countries.cid = SUBSTR(matches.teams,1,3)', 'name')
->where(array('round'=>1));
This gives an error.
SQLSTATE[42000]: Syntax error or access violation: 1630 FUNCTION mydatabase.SUBSTR does not exist.
Check the 'Function Name Parsing and Resolution' section in the Reference Manual
I've run the mysql query corresponds to this and it works perfectly.
SELECT `matches`.`match_code`, `countries`.`name`
FROM `matches` INNER JOIN `countries`
ON `countries`.`cid`=SUBSTR(`matches`.`teams`,1,3)
WHERE `round` = 1
I cannot use Zend\Db\Sql\Expression in here as the join() expects only a string.
Any suggestions??
I have solved this issue by using Zend\Db\Sql\Expression in the ON clause of join().
$select->columns(array('match_code'))
->join('countries',new \Zend\Db\Sql\Predicate\Expression('countries.cid = SUBSTR(matches.teams,1,3)'), 'name')
->where(array('round'=>1));
Thanks to #Crisp for the suggestion.
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?