4

我有一个看起来像这样的表:

mysql> SELECT * FROM Colors;
╔════╦══════════╦════════╦════════╦════════╦════════╦════════╦════════╗
║ ID ║ USERNAME ║  RED   ║ GREEN  ║ YELLOW ║  BLUE  ║ ORANGE ║ PURPLE ║
╠════╬══════════╬════════╬════════╬════════╬════════╬════════╬════════╣
║  1 ║ joe      ║ 1      ║ (null) ║ 1      ║ (null) ║ (null) ║ (null) ║
║  2 ║ joe      ║ 1      ║ (null) ║ (null) ║ (null) ║ 1      ║ (null) ║
║  3 ║ bill     ║ 1      ║ 1      ║ 1      ║ (null) ║ (null) ║ 1      ║
║  4 ║ bill     ║ (null) ║ 1      ║ (null) ║ 1      ║ (null) ║ (null) ║
║  5 ║ bill     ║ (null) ║ 1      ║ (null) ║ (null) ║ (null) ║ (null) ║
║  6 ║ bob      ║ (null) ║ (null) ║ (null) ║ 1      ║ (null) ║ (null) ║
║  7 ║ bob      ║ (null) ║ (null) ║ (null) ║ (null) ║ (null) ║ 1      ║
║  8 ║ bob      ║ 1      ║ (null) ║ (null) ║ (null) ║ (null) ║ (null) ║
╚════╩══════════╩════════╩════════╩════════╩════════╩════════╩════════╝

我想运行一个UPDATEandDELETE来查找并删除重复项并合并记录,以便我们以此作为结果结束。

mysql> SELECT * FROM Colors;
╔════╦══════════╦═════╦════════╦════════╦════════╦════════╦════════╗
║ ID ║ USERNAME ║ RED ║ GREEN  ║ YELLOW ║  BLUE  ║ ORANGE ║ PURPLE ║
╠════╬══════════╬═════╬════════╬════════╬════════╬════════╬════════╣
║  1 ║ joe      ║   1 ║ (null) ║ 1      ║ (null) ║ 1      ║ (null) ║
║  3 ║ bill     ║   1 ║ 1      ║ 1      ║ 1      ║ (null) ║ 1      ║
║  6 ║ bob      ║   1 ║ (null) ║ (null) ║ 1      ║ (null) ║ 1      ║
╚════╩══════════╩═════╩════════╩════════╩════════╩════════╩════════╝

我知道我可以使用脚本轻松完成此操作,但为了更好地学习和理解 MySQL,我想学习如何使用 pure 来完成此操作SQL

4

3 回答 3

3

这只是一个投影。它不会更新表,也不会删除一些数据。

SELECT  MIN(ID) ID,
        Username,
        MAX(Red) max_Red,
        MAX(Green) max_Green,
        MAX(Yellow) max_Yellow,
        MAX(Blue) max_Blue,
        MAX(Orange) max_Orange,
        MAX(Purple) max_Purple
FROM    Colors
GROUP   BY Username

更新

如果你真的想删除这些记录,你需要先运行 UPDATE 语句才能删除记录

UPDATE  Colors a
        INNER JOIN
        (
            SELECT  MIN(ID) min_ID,
                    Username,
                    MAX(Red) max_Red,
                    MAX(Green) max_Green ,
                    MAX(Yellow) max_Yellow,
                    MAX(Blue) max_Blue,
                    MAX(Orange) max_Orange,
                    MAX(Purple) max_Purple
            FROM    Colors
            GROUP   BY Username
        ) b ON a.ID = b.Min_ID 
SET     a.Red = b.max_Red,
        a.Green = b.max_Green,
        a.Yellow = b.max_Yellow,
        a.Blue = b.max_Blue,
        a.Orange = b.max_Orange,
        a.Purple = b.max_Purple

然后您现在可以删除记录,

DELETE  a
FROM    Colors a
        LEFT JOIN
        (
            SELECT  MIN(ID) min_ID,
                    Username
            FROM    Colors
            GROUP   BY Username
        ) b ON a.ID = b.Min_ID 
WHERE   b.Min_ID  IS NULL
于 2013-01-18T17:26:51.303 回答
1

你真的需要更新基础表吗?如果没有(并且您只是想要示例中显示的结果集),您可以简单地对表进行分组:

SELECT   MIN(ID)     AS ID,
         Username    AS Username,
         MAX(Red)    AS Red,
         MAX(Green)  AS Green,
         MAX(Yellow) AS Yellow,
         MAX(Blue)   AS Blue,
         MAX(Orange) AS Orange,
         MAX(Purple) AS Purple
FROM     Colors
GROUP BY Username

sqlfiddle上查看。

于 2013-01-18T17:26:47.947 回答
0
DELETE FROM Colors c1
WHERE EXISTS (SELECT 1
                FROM Colors c2
               WHERE c1.Username = c2.Username
                 AND ((c1.Red    IS NULL AND c2.Red    IS NULL) OR c1.Red    = c2.Red   )
                 AND ((c1.Green  IS NULL AND c2.Green  IS NULL) OR c1.Green  = c2.Green )
                 AND ((c1.Yellow IS NULL AND c2.Yellow IS NULL) OR c1.Yellow = c2.Yellow)
                 AND ((c1.Blue   IS NULL AND c2.Blue   IS NULL) OR c1.Blue   = c2.Blue  )
                 AND ((c1.Orange IS NULL AND c2.Orange IS NULL) OR c1.Orange = c2.Orange)
                 AND ((c1.Purple IS NULL AND c2.Purple IS NULL) OR c1.Purple = c2.Purple)
                 AND c2.ID < c1.ID
             )

空值使这更复杂一些,因为 NULL = NULL 不正确,但在 SQL 中是未知的。如果您有 0 和 1,则可以省略颜色条件中 OR 之前的部分。

于 2013-01-18T17:34:28.140 回答