MySQL - 基于多个表上的 SELECT 查询编写 UPDATE 查询

标签 mysql sql sql-update mysql-error-1064

我在 MySQL 或数据库方面还远远不够,所以我正在使用一个名为 FlySpeed SQL Query 的工具。这个工具帮助我以图形方式创建 MySQL 查询。 这是我使用此工具创建的查询,并在互联网上进行了大量阅读。

Select
  Employee.Firstname As Prénom,
  Employee.Name As NOM,
  TimeSheet.Filled As Validé,
  TimeSheet.Closed As Clôturé,
  Sum(Imputation.Hours) As `Somme des heures`,
  TimeSheet.Month + 1 As Mois,
  TimeSheet.Year As Année
From
  Employee Inner Join
  TimeSheet On TimeSheet.Employee_Id = Employee.Id Inner Join
  Imputation On Imputation.TimeSheet_Id = TimeSheet.Id Inner Join
  Project On Imputation.Project_Id = Project.Id
Where
  TimeSheet.Filled = '1' And
  (TimeSheet.Closed = '0' Or
    TimeSheet.Closed Is Null) And
  Imputation.Day <= Last_Day(Current_Date - Interval 1 Month) And Imputation.Day >= Date_Format(Current_Date - Interval 1 Month, '%Y-%m-01') And
  Project.Id != '1'
Group By
  Employee.Name, TimeSheet.Month + 1, TimeSheet.Year
Having
  Sum(Imputation.Hours) >= 5 * ((DateDiff(Last_Day(Current_Date - Interval 1 Month), Date_Format(Current_Date - Interval 1 Month, '%Y-%m-01')) + 1))
Order By
  Année,
  Mois,
  NOM

此查询返回的正是我想要的结果。保持与上面的 MySQL 查询相同的条件,我想将 Closed 字段更新为“1”。我想做一些那里有味道的东西:

-- UPDATE Query
--
UPDATE TimeSheet
SET Closed = '1'
--
-- UPDATE Query
From
Employee Inner Join
TimeSheet On TimeSheet.Employee_Id = Employee.Id Inner Join
Imputation On Imputation.TimeSheet_Id = TimeSheet.Id Inner Join
Project On Imputation.Project_Id = Project.Id
--
-- With those conditions
--
Where
TimeSheet.Filled = '1' And
(TimeSheet.Closed = '0' Or
    TimeSheet.Closed Is Null) And
-- Calculating a time range
Imputation.Day <= Last_Day(Current_Date - Interval 1 Month) And Imputation.Day >= Date_Format(Current_Date - Interval 1 Month, '%Y-%m-01') 
And
Project.Id != '1'
Group By
Employee.Name, TimeSheet.Month + 1, TimeSheet.Year
Having
-- Calculation : >= 5 times the number of days in the period
Sum(Imputation.Hours) >= 5 * ((DateDiff(Last_Day(Current_Date - Interval 1 Month), Date_Format(Current_Date - Interval 1 Month, '%Y-%m-01')) + 1))
---
-- With those conditions

所以我需要帮助将 SELECT 查询转换为 UPDATE 查询。请随时向我询问更多信息。

最佳答案

尝试将set操作移到joins之后:

UPDATE TimeSheet Inner Join 
   Employee On TimeSheet.Employee_Id = Employee.Id Inner Join
   Imputation On Imputation.TimeSheet_Id = TimeSheet.Id Inner Join
   Project On Imputation.Project_Id = Project.Id
--
-- With those conditions
--
SET TimeSheet.Closed = '1'
Where
    TimeSheet.Filled = '1' And
    (TimeSheet.Closed = '0' Or
    TimeSheet.Closed Is Null) And
    -- Calculating a time range
    Imputation.Day <= Last_Day(Current_Date - Interval 1 Month) And Imputation.Day >= Date_Format(Current_Date - Interval 1 Month, '%Y-%m-01') 
    And
    Project.Id != '1'
Group By
    Employee.Name, TimeSheet.Month + 1, TimeSheet.Year
Having
    -- Calculation : >= 5 times the number of days in the period
    Sum(Imputation.Hours) >= 5 * ((DateDiff(Last_Day(Current_Date - Interval 1 Month), Date_Format(Current_Date - Interval 1 Month, '%Y-%m-01')) + 1))
    ---

关于MySQL - 基于多个表上的 SELECT 查询编写 UPDATE 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28458386/

相关文章:

mysql - 只检索每个实体的最后一条记录,而不是所有记录

mysql - 更新 'tableA JOIN tableB' 仅适用于第一组(条件)

php - MySQL 多表更新语句

c# - 我的 asp.net Web 应用程序中使用 C# 的 mysql 出现超时过期错误?

mysql - 这个mysql创建表查询有什么问题?

sql - 添加/删除表格的列 - 代码维护/优化

php - MySQL 只更新一半的查询

mysql - PG 尝试将 rails db 推送到 heroku 时出错

mysql - MySQL 中的多个连接

sql - PL/pgSQL : merge results from iterative function call in another function