当我将ubuntu从15.10升级到16.04时,我的yii2项目中出现了这个错误
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3
of SELECT list is not in GROUP BY clause and contains nonaggregated column
'iicityYii.opportunity_conditions.money' which is not functionally dependent
on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
正在执行的SQL是:
SELECT SUM(oc.money),op.id,oc.money,
op.mantaghe,
op.`time`, op.`id`, `op`.`logo`,
`pd`.`user_id`, `op`.`name`,
`pd`.`co_name`, `op`.`address`,
`op`.`project_type_id`, `op`.`state_id`
FROM `opportunity` op
INNER JOIN `profile_details` pd ON op.user_id=pd.user_id
INNER JOIN `opportunity_conditions` oc ON op.id=oc.opportunity_id
GROUP BY `op`.`id`
ORDER BY `op`.`id` DESC
如何解决我的问题?
答案 0 :(得分:56)
运行:
sudo mysql -u root -p
并更改MySQL服务器实例的SQL模式:
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
另一种方法是使用MySQL配置。转到/etc/mysql/my.cnf
:
[mysqld]
并在其下方添加声明
sql_mode = ""
重启mysql服务:
sudo systemctl restart mysql
答案 1 :(得分:10)
在使用MySql的laravel中转到文件config / database.php,它将数组MySql mode strict更改为false。
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false, //from true
'engine' => null,
],
],
答案 2 :(得分:7)
在您的选择中,您有一个聚合函数和一组列名称,该错误告诉您没有在group by子句中指定正确的列名列表。可能是你应该在组中添加更多的列名称,可能与profile_details, opportunity_conditions
表
您还有,(opportunity.id),(opportunity_conditions.money),
(opportunity.mantaghe),
为什么()
如果您需要总和,则必须向所有列添加总和
sum(opportunity.id), sum(opportunity_conditions.money),
和(opportunity.mantaghe),
否则,如果这些是普通列,则应使用正常的sintax而不使用()
opportunity.id, opportunity_conditions.money,opportunity.mantaghe,
我试图重写可能的查询
SELECT SUM(opportunity_conditions.money),
`opportunity`.`id`,
`opportunity_conditions.money`,
`opportunity.mantaghe`,
`opportunity`.`time`,
`opportunity`.`logo`,
`profile_details`.`user_id`,
`opportunity`.`name`,
`profile_details`.`co_name`,
`opportunity`.`address`,
`opportunity`.`project_type_id`,
`opportunity`.`state_id`
FROM `opportunity`
INNER JOIN `profile_details` ON `opportunity`.`user_id`= `profile_details`.`user_id` 7
INNER JOIN `opportunity_conditions` ON `opportunity`.`id`=`opportunity_conditions`.`opportunity_id`
GROUP BY`opportunity`.`id`, `profile_details`.`user_id`,`opportunity_conditions.money`,
ORDER BY `opportunity`.`id` DESC
with group by the essential column name(i hope)
GROUP BY`opportunity`.`id`, `profile_details`.`user_id`,`opportunity_conditions.money`,
答案 3 :(得分:6)
请复制此行并运行它。它对我有用。
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
答案 4 :(得分:3)
解决方案是编辑MySQL配置文件,因为配置将在每次重新启动后恢复...
class Species(models.Model):
name = models.CharField()
class SpeciesAttribute(models.Model):
species = models.ForeignKey(Species)
name = models.CharField()
class Animal(models.Model):
name = models.CharField()
species = models.ForeignKey(Species)
class AnimalAttributeValue(models.Model):
animal = models.ForeignKey(Animal)
attribute = models.ForeignKey(SpeciesAttribute)
value = models.CharField()
并添加
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
然后重新启动
[mysqld]
sql-mode=""
在ubuntu 18.04上工作。
答案 5 :(得分:1)
对于CentOS8,我使用webmin配置文件/etc/my.cnf.d/mysql-server.cnf
[mysqld] sql-mode =“”
答案 6 :(得分:0)
谢谢,这对我有帮助,但这不会将其设置为永久,并且每次重启后都会恢复。
所以你应该在配置文件中设置它(例如[mysqld]部分中的/etc/mysql/my.cnf),以便在MySQL重启后更改仍然有效:
配置文件:/etc/mysql/my.cnf
[mysqld]
sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
答案 7 :(得分:0)
只需对以下内容严格设置为假
Config > database.php set 'strict' => false,
答案 8 :(得分:0)
您可以将 select id, level_cd_1 as level_cd from Test_2020 where level_cd_1 is not null
union all
select id, level_cd_2 from Test_2020 where level_cd_2 is not null
用于未聚合的列。因此,您可以使用:
select id, coalesce(level_cd_1,level_cd_2)as leve_cd from Test_2020 where
(level_cd_1 is not null or level_cd_2 is not null)
更多详细信息here。
答案 9 :(得分:0)
将查询更新为:
SELECT SUM(any_value(oc.money)),op.id,any_value(oc.money) as money,
op.mantaghe,
op.`time`, op.`id`, `op`.`logo`,
any_value(`pd`.`user_id`) as user_id, `op`.`name`,
any_value(`pd`.`co_name`) as co_name, `op`.`address`,
`op`.`project_type_id`, `op`.`state_id`
FROM `opportunity` op
INNER JOIN `profile_details` pd ON op.user_id=pd.user_id
INNER JOIN `opportunity_conditions` oc ON op.id=oc.opportunity_id
GROUP BY `op`.`id`
ORDER BY `op`.`id` DESC
了解
当查询包含group by
时,您只应从要分组的表中选择列,否则,应使用any_value()
函数从其他表中调用列。
我个人尝试不更新sql_mode全局变量。
您可以在https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
上了解更多信息