gpt4 book ai didi

sql - 如何删除最小值重复项?

转载 作者:行者123 更新时间:2023-11-29 14:20:27 26 4
gpt4 key购买 nike

我有一个包含以下列的表格:

  id      name    value
serial text text

我需要删除具有最小 id 值和 name = 'SUPPORT_EMAIL' 的行。我怎样才能以最佳方式做到这一点?我试过:

DELETE FROM propertie 
WHERE name = 'SUPPORT_EMAIL'
HAVING id = MIN(id)
GROUP BY name ;

DELETE FROM propertie 
WHERE name = 'SUPPORT_EMAIL'
GROUP BY name
HAVING id = MIN(id) ;

但是没有用。当然可以写

DELETE FROM propertie 
WHERE id = (SELECT id
FROM propertie
WHERE name = 'SUPPORT_EMAIL' and id = (SELECT MIN(id)
FROM propertie
WHERE name = 'SUPPORT_EMAIL')
)

但我觉得这很奇怪。

最佳答案

你已经很接近了,但可以通过以下方式简化你的工作:

DELETE FROM propertie 
WHERE id = (SELECT MIN(id)
FROM propertie
WHERE name = 'SUPPORT_EMAIL')

您还可以:

DELETE FROM propertie 
WHERE id = (SELECT id
FROM propertie
WHERE name = 'SUPPORT_EMAIL'
ORDER BY id
LIMIT 1)

我不确定这是否是合法的 PostgreSQL 语法:

DELETE FROM   propertie 
WHERE name = 'SUPPORT_EMAIL'
ORDER BY id
LIMIT 1

关于sql - 如何删除最小值重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29511677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com