用 Max 删除

发布于 2024-09-17 12:35:13 字数 358 浏览 8 评论 0原文

这是基于我的上一个问题

我有下表

Table1

JobPositionId | JobPositionName
     1        |      Sound
     2        |      Lights
     3        |      Sound
     4        |      Ground

如何删除第三行(名称 = 声音和最大位置)

This is based on my previous question.

I have the following table

Table1

JobPositionId | JobPositionName
     1        |      Sound
     2        |      Lights
     3        |      Sound
     4        |      Ground

How can I delete row three (Name = sound, and max position)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糖 2024-09-24 12:35:13

使用:

DELETE FROM TABLE t1 
       JOIN (SELECT x.jobpositionname,
                    MAX(jobPositonId) AS max_id 
               FROM TABLE x
           GROUP BY x.jobpositionname) t2 
 WHERE t1.jobPositonId  = t2.max_id
   AND t1.jobpositionname = t2.jobpositionname
   AND t2.jobpositionname = 'Sound'

正如我在上一个问题中提到的,使用它不会触发 MySQL 1093 错误:

DELETE FROM TABLE
 WHERE JobPositionId = SELECT x.id
                         FROM (SELECT MAX(JobPositionId) AS id
                                 FROM TABLE
                                WHERE JobPositionName = 'Sound') x

Use:

DELETE FROM TABLE t1 
       JOIN (SELECT x.jobpositionname,
                    MAX(jobPositonId) AS max_id 
               FROM TABLE x
           GROUP BY x.jobpositionname) t2 
 WHERE t1.jobPositonId  = t2.max_id
   AND t1.jobpositionname = t2.jobpositionname
   AND t2.jobpositionname = 'Sound'

As I mentioned in your previous question, using this won't trigger a MySQL 1093 error:

DELETE FROM TABLE
 WHERE JobPositionId = SELECT x.id
                         FROM (SELECT MAX(JobPositionId) AS id
                                 FROM TABLE
                                WHERE JobPositionName = 'Sound') x
逐鹿 2024-09-24 12:35:13
DELETE FROM
    Table1
WHERE
    JobPositionId = (
        SELECT
            MAX(JobPositionId)
        FROM
            Table1
        WHERE
            JobPositionName = 'Sound'
    )
DELETE FROM
    Table1
WHERE
    JobPositionId = (
        SELECT
            MAX(JobPositionId)
        FROM
            Table1
        WHERE
            JobPositionName = 'Sound'
    )
将军与妓 2024-09-24 12:35:13

抱歉,如果这没有考虑到您的“上一个问题”,我想我只想看看这个。

DELETE FROM Table1 WHERE jobpositionid = (SELECT MAX(jobpositionid) FROM table1 WHERE name = 'Sound');

Sorry if this doesn't take into account your "previous question" thought I'd just look at this one.

DELETE FROM Table1 WHERE jobpositionid = (SELECT MAX(jobpositionid) FROM table1 WHERE name = 'Sound');
染火枫林 2024-09-24 12:35:13

看起来您想要做的是删除所有重复的 JobPositionNames,只留下 JobPositionId 最低的那个。

我最近不得不做一些非常类似的事情,发现 SQL 语句变得如此复杂,用 SQL 来做要容易得多(虽然效率低得多)。

因此,如果您要尝试清理这是一个被污染的数据库,只需编写一个脚本来完成此操作并完成它(并设置一些唯一索引以防止它再次发生)。

如果这种情况一直发生,并且需要定期完成,请修复执行此操作的代码。

It seems like what you are trying to do is to delete all duplicate JobPositionNames, leaving only the one with the lowest JobPositionId.

I had to do something very similar recently and found the SQL statements getting so complicated, it was much much easier (if much less efficient) to do it in SQL.

So in case this is a poluted database you're trying to clean, just write a script that does this and be done with it (and set some unique indexes to prevent it from happening again).

If this happens all the time, and needs to be done periodicaly, fix the code that does this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文