【问题标题】:How can I downgrade a table reference from mysql 8.0* to 5.5*如何将表引用从 mysql 8.0* 降级到 5.5*
【发布时间】:2022-01-11 13:52:16
【问题描述】:

我在我的 mysql 8.0 数据库上使用了一个表引用:

update station_temp_data a
   set min_temp = (select min_temp from 
                      ( select min(air_temp) as min_temp
                        from station_temp_data b
                        where b.station_id = a.station_id
                        and DATE_FORMAT(b.mdate, "%Y.%m.%d") = DATE_FORMAT(a.mdate, "%Y.%m.%d")) as Q1) 

这在 mysql 8.0 上可以正常工作,但在 5.5 上不行!

我在 5.5 上报错:

Error : Unknown column 'a.station_id' in 'where clause'

如何将此 SQL 降级到 5.5 版本?

【问题讨论】:

  • 您必须使用多表 UPDATE,而不是相关子查询。在提到的两个版本中。
  • 太棒了!你有示例链接吗?
  • 我投票结束这个问题,因为这个问题与编程无关

标签: mysql


【解决方案1】:

首先对于 mysql 8.0,您必须禁用 ONLY_FULL_GROUP_BY(s.Disable ONLY_FULL_GROUP_BYWhat are the benefits of only_full_group_by mode?

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

接下来的步骤适用于 8.0 和 5.5!

然后创建一个包含每日最低温度的视图:

create VIEW min_temp_day as
select 
   min(AIR_TEMPERATURE_C) min_temp, DATE_FORMAT(mdate, "%Y.%m.%d") days, 
   air_temp, station_id 
from station_temp_data
GROUP BY station_id,days

有了这个视图,更新很简单,没有复杂的加入或其他倒立:

update station_temp_data a
set min_temp = (select min_temp from min_temp_day b
                where  days = DATE_FORMAT(a.dtg, "%Y.%m.%d") 
                       and b.STATION_ID = a.station_id)

这个版本比问题中的要快得多!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多