从mysql数据库中只删除一条记录的正确方法是什么。 此查询仅用于选择一条记录。
SELECT * FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
上面的查询运行正确。但是当替换select时删除那些没有用的。
DELETE FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
我该如何解决?
答案 0 :(得分:4)
如果您有id列,则可以使用subselect。我删除了订单,因为这与order by 'Mexico' asc
相同,这是非常无用的。
DELETE FROM Customers
WHERE (CustomerID IN ( SELECT CustomerID
FROM Customers where country = 'Mexico'
ORDER BY CustomerID ASC LIMIT 1 )) ;
答案 1 :(得分:0)
我认为以下查询会对您有所帮助。您需要使用一些密钥ID来区分。
DELETE FROM Customers
WHERE SOME_KEY_ID IN
(
SELECT SOME_RANDOM_ID FROM
(
SELECT B.SOME_KEY_ID SOME_RANDOM_ID FROM Customers as B
where Country = 'Mexico'
LIMIT 1
) as c
) ;
注意:内部选择SOME_RANDOM_ID是必需的,否则sqlfiddle会抛出错误This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery':
。