【问题标题】:Delete subquery with alias MySQL删除别名 MySQL 的子查询
【发布时间】:2015-11-12 10:32:40
【问题描述】:

我对此删除查询有疑问:

DELETE r 
FROM table AS r 
WHERE r.stoptime IS NULL 
  and r.address IN 
    (select address from table where starttime <= r.starttime and stoptime > r.starttime) 

我收到以下错误:

错误:您不能在 FROM 子句中指定目标表 'r' 进行更新。

我的目标是删除开始时间包含在另一条记录中的记录,但子查询中的别名出错。

有人知道怎么做吗?提前致谢。

【问题讨论】:

    标签: mysql subquery alias


    【解决方案1】:

    尝试像这样使用 JOINS:

     DELETE r
      FROM `table` r 
      JOIN `table` t ON t.id = r.id
     WHERE t.starttime <= r.starttime and t.stoptime > r.starttime
       AND r.stoptime IS NULL
    

    【讨论】:

    • 我认为这段代码非常适合我,但另一个问题,我不确定是否可能:我想添加 ORDER BY username LIMIT 100 子句。但我有错误。有可能吗?
    • @AndroidLearner:- 不,你不能使用它。见Delete docsFor the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY 和 LIMIT 不能使用。
    • 非常感谢。你拯救了我的一天。谢谢。
    猜你喜欢
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多