简体   繁体   中英

update with select and case statement in mysql

can anyone help me with this query what is going wrong in this query.

Query

update vcd_deals
set del_date='2015-09-17'
where case
      when (
              (select count(del_id)
               from vcd_deals
               where del_date=curdate()+1
                 and del_superSaver_deal=1)=0) then 1
      else 0
  end
order by del_date asc limit 3

I am getting the following error:

Error

You can't specify target table 'vcd_deals' for update in FROM clause.

Actually I want to update first three rows of vcd_deals table only when (select count(del_id) from vcd_deals where del_date=curdate()+1 and del_superSaver_deal=1)=0 .

please suggest is there any other way to do this. Thanks

That is the problem with MySQL Parser. You can try Inner Join instead.

Otherwise you can enclose your subselect into a separate table like below:(not tested)

update vcd_deals
set del_date='2015-09-17'
where 0 = ( select Del_id_count 
             from (select count(del_id) as Del_id_count
                   from vcd_deals
                   where del_date=curdate()+1
                     and del_superSaver_deal=1) temp
                    )
order by del_date asc limit 3

You can try as per below-

UPDATE vcd_deals 
SET del_date='2015-09-17' 
WHERE del_date<>ADDDATE(CURDATE(), INTERVAL 1 DAY) 
AND del_superSaver_deal<>1 
ORDER BY del_date LIMIT 3;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM