如何在MySQL中删除2列作为复合主键的多行?

时间:2022-06-18 23:11:23

My innodb table has the following structure: 4 columns (CountryID, Year, %Change, Source), with the 2 columns (CountryID, Year) as the primary key. How do I delete multiple rows other than using a for-loop to delete each row?

我的innodb表具有以下结构:4列(CountryID,Year,%Change,Source),其中2列(CountryID,Year)作为主键。除了使用for循环删除每一行之外,如何删除多行?

I'm looking for something similar to

我正在寻找类似的东西

DELETE FROM CPI 
 WHERE CountryID AND Year IN (('AD', 2010), ('AF', 2009), ('AG', 1992))

Found the answer after further tweaks:

进一步调整后找到答案:

DELETE FROM CPI 
 WHERE (CountryID, Year) IN (('AD', 2010), ('AF', 2009), ('AG', 1992))

Hope this helps anyone out there in the same pickle.

希望这可以帮助任何人在同一个泡菜。

1 个解决方案

#1


29  

The answer in Oracle is:

Oracle的答案是:

delete from cpi
 where (countryid, year) in (('AD', 2010), ('AF', 2009), ('AG', 1992))

It's fairly standard SQL syntax and I think MySQL is the same.

它是相当标准的SQL语法,我认为MySQL是相同的。

#1


29  

The answer in Oracle is:

Oracle的答案是:

delete from cpi
 where (countryid, year) in (('AD', 2010), ('AF', 2009), ('AG', 1992))

It's fairly standard SQL syntax and I think MySQL is the same.

它是相当标准的SQL语法,我认为MySQL是相同的。