Update two columns of two different table in single query with order by and limit-mysql

为君一笑 提交于 2019-12-12 06:19:27

问题


Query

 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

Error

#1221 - Incorrect usage of UPDATE and ORDER BY

what is wrong in this and any other way to do this


回答1:


There is no sense of using ORDER BY in Update. Because the command updates all the matching orders no matter in what sequence.

Use Update command without ORDER BY clause as:

 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



回答2:


i got solution of this question

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)

i have placed one more condition which replaced order by and limit



来源:https://stackoverflow.com/questions/32583220/update-two-columns-of-two-different-table-in-single-query-with-order-by-and-limi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!