Aabid
Aabid

Reputation: 941

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

Upvotes: 0

Views: 115

Answers (2)

Arun Palanisamy
Arun Palanisamy

Reputation: 5459

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

Upvotes: 1

Zafar Malik
Zafar Malik

Reputation: 6844

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;

Upvotes: 0

Related Questions