根据最大值删除行
如何构建 mySQL 查询以根据最大值删除行。
我尝试了
WHERE jobPositonId = max(jobPostionId)
但出现错误?
How can I structure a mySQL query to delete a row based on the max value.
I tried
WHERE jobPositonId = max(jobPostionId)
but got an error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
请注意,如果存在重复项,则具有该 jobPositonId 值的所有行都将被删除。
关于 1093 错误的愚蠢部分是,您可以通过在自引用之间放置一个子查询来绕过它:
解释
MySQL 仅在使用 UPDATE & 时进行检查。
DELETE
语句,如果正在更新的同一个表存在第一级子查询。这就是为什么将其放入第二级(或更深层)子查询替代方案中有效的原因。但它只是检查子查询 - JOIN 语法在逻辑上是等效的,但不会触发错误。Use:
Mind that all the rows with that
jobPositonId
value will be removed, if there are duplicates.The stupid part about the 1093 error is that you can get around it by placing a subquery between the self reference:
Explanation
MySQL is only checking, when using
UPDATE
&DELETE
statements, if the there's a first level subquery to the same table that is being updated. That's why putting it in a second level (or deeper) subquery alternative works. But it's only checking subqueries - the JOIN syntax is logically equivalent, but doesn't trigger the error.或者
OR
这是可行的:
除了两次访问数据库之外,这种技术还有什么问题吗?
This works:
Other than going to the database twice, is there anything wrong with this technique?