简体   繁体   English

使用 MySQL db 和 python 客户端执行 sql 查询时出错

[英]Error while executing a sql query using MySQL db and python client

I have a complex sql query as below which I am using to access MySQL db from a python script.我有一个复杂的 sql 查询,如下所示,我使用它从 python 脚本访问 MySQL 数据库。

 sql_query_vav = """SELECT t1.deviceId, t1.date, t1.vavId, t1.timestamp, t1.nvo_airflow as airflow, t1.nvo_air_damper_position as damper_position , t1.nvo_temperature_sensor_pps as vavTemperature , d.MILO as miloId ,m1.timestamp as miloTimestamp, m1.temperature as miloTemperature
                   FROM
                       (SELECT deviceId, date, nvo_airflow, nvo_air_damper_position, nvo_temperature_sensor_pps, vavId, timestamp, counter from vavData where date=%s and floor=%s) t1
                   INNER JOIN
                        (SELECT date,max(timestamp) as timestamp,vavId from vavData where date=%s and floor=%s group by vavId) t2
                   ON (t1.timestamp = t2.timestamp)     
                   INNER JOIN 
                       (SELECT VAV,MILO,floor from VavMiloMapping where floor = %s) d
                   ON (t1.vavId = d.VAV )
                   INNER JOIN
                         (SELECT t1.deviceId,t1.date,t1.timestamp,t1.humidity,t1.temperature,t1.block,t1.floor,t1.location
                         FROM
                             (SELECT deviceId,date,timestamp,humidity,temperature,block,floor,location from miloData WHERE date=%s and floor=%s) t1
                         INNER JOIN
                             (SELECT deviceId,max(timestamp) as timestamp,location from miloData where date=%s and floor=%s GROUP BY deviceId) t2
                         ON (t1.timestamp = t2.timestamp)) m1
                   ON (d.MILO = m1.location)  order by t1.vavId"""

I get an error with the above query which says上面的查询出现错误,上面写着

mysql.connector.errors.ProgrammingError: 1055 (42000): Expression #3 of SELECT list is not in GROUP BY 
clause and contains nonaggregated column 'minniedb.miloData.location' which is not functionally dependent
on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 

I have tried to change the sql mode by executing我试图通过执行来改变 sql 模式

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

and tried to restart the mysql service using并尝试使用重新启动mysql服务

sudo service mysql restart

I think I have done everything required why I am not sure why I am still getting the same error.我想我已经完成了所需的一切,为什么我不确定为什么我仍然遇到同样的错误。 Can some one please help me with this.有人可以帮我解决这个问题吗?

If you want to find the place of uncorrectness you must format the code carefully at least.如果您想找到不正确的地方,您至少必须仔细格式化代码。

SELECT t1.deviceId, 
       t1.date, 
       t1.vavId, 
       t1.timestamp, 
       t1.nvo_airflow as airflow, 
       t1.nvo_air_damper_position as damper_position , 
       t1.nvo_temperature_sensor_pps as vavTemperature , 
       d.MILO as miloId ,
       m1.timestamp as miloTimestamp, 
       m1.temperature as miloTemperature
FROM ( SELECT deviceId, 
              date, 
              nvo_airflow, 
              nvo_air_damper_position, 
              nvo_temperature_sensor_pps, 
              vavId, 
              timestamp, 
              counter 
       from vavData 
       where date=%s 
         and floor=%s
     ) t1
INNER JOIN ( SELECT date,
                    max(timestamp) as timestamp,
                    vavId 
             from vavData 
             where date=%s 
               and floor=%s 
             group by vavId
           ) t2 ON (t1.timestamp = t2.timestamp)     
INNER JOIN ( SELECT VAV,
                    MILO,
                    floor 
             from VavMiloMapping 
             where floor = %s 
           ) d ON (t1.vavId = d.VAV )
INNER JOIN ( SELECT t1.deviceId,
                    t1.date,
                    t1.timestamp,
                    t1.humidity,
                    t1.temperature,
                    t1.block,
                    t1.floor,
                    t1.location
             FROM ( SELECT deviceId,
                           date,
                           timestamp,
                           humidity,
                           temperature,
                           block,
                           floor,
                           location 
                    from miloData 
                    WHERE date=%s 
                      and floor=%s
                  ) t1
             INNER JOIN ( SELECT deviceId,
                                 max(timestamp) as timestamp,
                                 location 
                          from miloData 
                          where date=%s 
                            and floor=%s 
                          GROUP BY deviceId
                        ) t2 ON (t1.timestamp = t2.timestamp)
           ) m1 ON (d.MILO = m1.location)  
order by t1.vavId

Now it is visible that there are 2 points of uncorrectness.现在可以看出有 2 点不正确。 Both problematic subqueries have an alias t2 and looks like两个有问题的子查询都有一个别名t2 ,看起来像

SELECT some_Id,
       max(timestamp) as timestamp,
       some_another_field
from some_table
where some_conditions
GROUP BY some_Id

The fiels marked as some_another_field is included into neither GROUP BY expression not aggregate function.标记为some_another_field既不包含在 GROUP BY 表达式中也不包含在聚合函数中。

Correct these subqueries.更正这些子查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM