2011-02-08 145 views
0

我目前的问题如下。 下面的选择脚本完美地工作。删除mysql查询

SELECT * 
FROM orderheader, orderbody, orderbodylocation 
WHERE orderheader.DateCreated > '2011-02-01 00:00:00' 
AND orderheader.OrderID = orderbody.OrderID 
AND orderbody.OrderBodyID = orderbodylocation.OrderBodyID 
AND orderbody.OrderID = orderbodylocation.OrderID 
AND orderheader.Status <= 1 

但低于删除查询这是where子句的复制失败围绕dateCreated会子句。

DELETE 
FROM orderheader, orderbody, orderbodylocation 
WHERE orderheader.DateCreated > '2011-02-01 00:00:00' 
AND orderheader.OrderID = orderbody.OrderID 
AND orderbody.OrderBodyID = orderbodylocation.OrderBodyID 
AND orderbody.OrderID = orderbodylocation.OrderID 
AND orderheader.Status <= 1 

orderheader表内的DateCreated字段是DATETIME类型。

我试图删除每个订单更新,然后1-2-2011(d-m-y)以及订购人关联的表数据在orderbody和orderbodylocation表中。

我已经用完了想法,而且我还没有太多有关MySQL DateTime数据类型字段的经验。 如果任何人都可以看到我的错误,将不胜感激,如果你可以让我知道我做错了什么。

+0

什么是错误? – 2011-02-08 02:06:46

+2

[offtop]当外键约束与删除级联是一个很好的工具[/ offtop] – zerkms 2011-02-08 02:11:18

回答

0

发布错误消息会很有帮助!

我不知道mysql,但如果它像其他SQL一样,不能在DELETE命令的FROM子句中引用多个表。

如果您有级联删除属性的外键,那么只需删除标题行,其余的将自动删除。

如果您没有为相关表设置级联删除,那么您将需要执行3 删除语句,从下往上删除数据。对于较低级别的表,您将需要使用子选择来匹配表头。以下是适用于顺序体的示例:

DELETE FROM orderbody其中orderbody.OrderID在(选择orderHeader.OrderID FROM orderheader WHERE orderheader.DateCreated> '2011-02-01 00:00:00' AND orderheader.Status < = 1)

HTH

利奥

1

如果手册页到zerkms都礼貌张贴链接适用于您的MySQL版本,那么你只需要添加(再次)表( s)你想从删除指令。你需要把它们放在DELETEFROM之间,像这样:

DELETE table1, table2, ... /* probably some or all tables in the FROM list */ 
FROM orderheader, orderbody, orderbodylocation 
...