我在MySQL或数据库方面还不够强大,所以我使用了一个名为FlySpeed SQL Query的工具。这个工具帮助我以图形方式创建MySQL查询。 这是我用这个工具创建的查询和大量的在互联网上阅读。MySQL - 在多个表上编写基于SELECT查询的UPDATE查询
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查询。随时问我更多的信息。
而且它不工作?第一眼看起来确定,假设你想更新每个返回的记录。 – LCIII 2015-02-11 15:45:53
当我按照我的想法尝试查询时:'ERROR 1064(42000):你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在第3行的'From Employee Inner Join TimeSheet On TimeSheet.Employee_Id = Employee.Id Inner'附近使用正确的语法(我已取消注释在使用前在此处发布的查询)。 – axsoverflow 2015-02-13 13:59:28