如何在mysql中删除数量大于1的货币?

时间:2017-08-22 15:15:27

标签: php mysql

我有一张名为 currency_country 的表,可以保存所有货币。一些欧洲国家也有旧货币。我想从拥有欧元货币的国家/地区删除所有非欧元货币?

我尝试过这种方式,但没有帮助!

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

2 个答案:

答案 0 :(得分: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
);

自我解释,您删除的所有货币都不是' EUR'以及货币' EUR'存在于同一个国家。

答案 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';