2015-09-15 97 views
0

任何人都可以使用此查詢來幫助我查詢中出現什麼問題。在mysql中使用select和case語句進行更新

查詢

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 

我收到以下錯誤:

錯誤

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

其實我想更新vcd_deals表的前三行 (select count(del_id) from vcd_deals where del_date=curdate()+1 and del_superSaver_deal=1)=0只有當。

請建議是否有任何其他方式來做到這一點。 感謝

回答

1

即與MySQL分析器的問題。您可以嘗試使用Inner Join

否則,你可以附上你的子查詢到一個單獨的表像如下:(未測試)

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 
+0

它的工作感謝兄弟,我有一個問題,你爲什麼使用兩個次選擇查找'Del_id_count' – Aabid

+0

這是派生表,它有助於提高性能。查看鏈接[Link1](http://www.databasejournal.com/features/mysql/article.php/3914401/Use-Derived-Tables-in-Your-MySQL-Queries-to-Improve-Database-Performance。 htm),[Link2](http://forums.devshed.com/mysql-help-4/subquery-vs-derived-table-944181.html)以獲取更多詳細信息 – Crazy2crack

+0

Hello @ Crazy2crack您可以在另一個查詢中幫助我嗎? 。我想用這個查詢更新兩個表列。 – Aabid

0

您可以嘗試按照如下─

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; 
+0

但我想更新從vcd_deals只有當'SELECT COUNT(del_id),其中del_date = CURDATE()+ 1和del_superSaver_deal = 1'這個查詢返回0 – Aabid

+0

是的,意味着你想排除滿足給定條件的行...更新查詢是不是做同樣的... –