使用order by和limit-mysql在单个查询中更新两个不同表的两列

时间:2015-09-15 10:05:12

标签: mysql insert-update

查询

 UPDATE vcd_resorts AS resorts, 
           vcd_deals AS deals 
    SET    resorts.rst_live_date = Date_add(Curdate(), INTERVAL 4 day), 
           deals.del_date = Date_add(Curdate(), INTERVAL 4 day) 
    WHERE  0 = (SELECT resort_id_count 
                FROM   (SELECT Count(rst_id) AS resort_id_count 
                        FROM   vcd_resorts 
                        WHERE  rst_supersaver_resort = 1 
                               AND rst_live_date BETWEEN Curdate() + 1 AND 
                                                         Curdate() + 4) 
                       temp) 
           AND resorts.rst_supersaver_resort = 1 
           AND resorts.rst_id = deals.del_resort_id 
           AND deals.del_supersaver_deal = 1 
    ORDER BY resorts.rst_live_date ASC 
    LIMIT 1

错误

  

#1221 - UPDATE和ORDER BY的使用不正确

这个和其他任何方式有什么问题

2 个答案:

答案 0 :(得分:0)

在Update中没有使用ORDER BY的意义。因为命令会更新所有匹配的顺序,无论顺序如何。

使用不带ORDER BY子句的更新命令:

 UPDATE vcd_resorts AS resorts, 
           vcd_deals AS deals 
    SET    resorts.rst_live_date = Date_add(Curdate(), INTERVAL 4 day), 
           deals.del_date = Date_add(Curdate(), INTERVAL 4 day) 
    WHERE  0 = (SELECT resort_id_count 
                FROM   (SELECT Count(rst_id) AS resort_id_count 
                        FROM   vcd_resorts 
                        WHERE  rst_supersaver_resort = 1 
                               AND rst_live_date BETWEEN Curdate() + 1 AND 
                                                         Curdate() + 4) 
                       temp) 
           AND resorts.rst_supersaver_resort = 1 
           AND resorts.rst_id = deals.del_resort_id 
           AND deals.del_supersaver_deal = 1 
    LIMIT 1

答案 1 :(得分:0)

我得到了这个问题的解决方案

UPDATE vcd_resorts AS resorts, 
                vcd_deals AS deals 
                SET    resorts.rst_live_date = Date_add(Curdate(), INTERVAL 4 day), 
                       deals.del_date = Date_add(Curdate(), INTERVAL 4 day) 
                WHERE  0 = (SELECT resort_id_count 
                            FROM   (SELECT Count(rst_id) AS resort_id_count 
                                    FROM   vcd_resorts 
                                    WHERE  rst_supersaver_resort = 1 
                                           AND rst_live_date BETWEEN Curdate() + 1 AND 
                                                                     Curdate() + 4) 
                                   temp) 
                AND resorts.rst_supersaver_resort = 1 
                AND resorts.rst_id = deals.del_resort_id 
                AND deals.del_supersaver_deal = 1 
                AND resorts.rst_id = (SELECT resort_id 
                                    FROM   (SELECT rst_id AS resort_id 
                                             FROM   vcd_resorts 
                                             WHERE  rst_supersaver_resort = 1 
                                             ORDER  BY rst_live_date ASC 
                                             LIMIT  1) temp1)

我已经再添加了一个条件,它取代了order by和limit