SQL删除最新记录 [英] SQL delete latest record

查看:75
本文介绍了SQL删除最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除指定产品ID和用户ID的最后一条记录。



我目前的代码是

从ShoppingBasket中删除top(1),其中productID = 1 AND userID =''admin''





但这会删除第一个而不是最后一条记录,并使用''order by desc''不起作用。

解决方案

 删除 来自 ShoppingBasket 其中 productID = 1  AND  userID = '  admin'  order   by (您的id  desc  limit  1  


这是另一种选择,但我最喜欢的是wizardzz解决方案。这基本上是wizzardzz解决方案的扩展,可能更容易理解!



  DELETE   FROM  shoppingBasket 
WHERE ID IN SELECT TOP 1
ID
FROM shoppingBasket
WHERE ProductID = 1
AND UserID = ' Admin'
ORDER BY ID DESC


I want to delete last record where product ID and userID are specified.

my current code is

delete top (1) from ShoppingBasket where productID=1 AND userID=''admin'' 



but this deletes the first not the last record, and using ''order by desc'' does not work.

解决方案

delete from ShoppingBasket where productID=1 AND userID='admin' order by (your id column) desc limit 1


Here is an alternative but I like wizardzz solution best. This basically an expansion of wizzardzz solution and may be easier to understand!

DELETE  FROM shoppingBasket
WHERE   ID IN (SELECT TOP 1
                      ID
               FROM   shoppingBasket
               WHERE  ProductID = 1
                      AND UserID = 'Admin'
               ORDER BY ID DESC)


这篇关于SQL删除最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆