2017-08-22 50 views
1

我有一張名爲currency_country的表,它包含所有貨幣。一些歐洲國家也有舊貨幣。我想從歐元國家的所有非歐元貨幣中刪除?如何刪除MySQL中計數大於1的貨幣?

我試過這些方法,但沒有幫助!

DELETE FROM `currency_country` 
WHERE currency_code NOT IN (
    SELECT currency_code 
    FROM (
    SELECT currency_code 
    FROM `currency_country` 
    WHERE currency_code = 'EUR' 
) temp 
); 

DELETE 
FROM 
    `currency_country` 
    LEFT JOIN 
     country 
     on `currency_country`.`country_id` = country.`id` 
WHERE 
    EXISTS 
    (
     SELECT 
     * 
     FROM 
     `currency_country` 
     WHERE 
     EXISTS 
     (
      SELECT 
       * 
      FROM 
       `currency_country` 
      WHERE 
       `currency_code` = 'EUR' 
     ) 
    ) 
    AND currency_code != 'EUR' 
    AND country.name IN 
    (
     'Austria', 
     'Belgium', 
     'Cyprus', 
     'Netherlands', 
     'Estonia', 
     'Finland', 
     'France', 
     'Germany', 
     'Greece', 
     'Ireland', 
     'Italy', 
     'Latvia', 
     'Lithuania', 
     'Luxembourg', 
     'Malta', 
     'Monaco', 
     'Portugal', 
     'San Marino', 
     'Slovakia', 
     'Slovenia', 
     'Spain' 
    ) 
; 

enter image description here

+0

'DELETE FROM currency_country WHERE COUNTRY_ID IN(SELECT COUNTRY_ID FROM currency_country WHERE CURRENCY_CODE = 'EUR')AND CURRENCY_CODE <> 'EUR'' – ctwheels

回答

1

你有沒有嘗試過這樣的事情:

DELETE FROM c1 
USING currency_country AS c1 
WHERE c1.currency_code != 'EUR' 
AND EXISTS (
    SELECT 1 
    FROM (SELECT * FROM currency_country) AS c2 
    WHERE c2.currency_code = 'EUR' 
    AND c1.country_id = c2.country_id 
); 

自我解釋,刪除每一個貨幣是不是 '歐元' 和在那裏同一國家存在貨幣「歐元」。

+2

第一'Where'條件應該是'WHERE c1.currency_code!=' EUR'',我想 – KubiRoazhon

+0

是的,錯別字對不起 – LP154

+0

這個我得到這個錯誤**你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到在'c1 '附近使用的正確語法WHERE c1.currency_code!='EUR' AND EXISTS( SELECT 1 FROM'curren'at line 1 ** –

1

另一種方法。找到所有歐元的國家ID,然後刪除所有非歐元貨幣。

DELETE FROM currency_country 
WHERE country_id IN (
    SELECT country_id 
    FROM currency_country 
    WHERE currency_code = 'EUR') 
AND currency_code <> 'EUR'; 

編輯

請試試這個。

DELETE FROM currency_country 
WHERE country_id IN (
    SELECT country_id 
    FROM (SELECT * FROM currency_country) AS something 
    WHERE currency_code = 'EUR') 
AND currency_code <> 'EUR'; 
+0

之前嘗試它表示**您無法在FROM子句中指定目標表'currency_country'進行更新** –

+0

試試your_db_name.currency_country。 – KubiRoazhon

+0

現在爲你工作? – waterloomatt