比较 2 个表格和 return MySQL 中的变化

Compare 2 tables and return the variations in MySQL

大家早上好,希望有人能帮我解答下面的问题

在MySQL中,我需要比较由不同员工填写的2个相同table和return,当记录中存在差异时,如果存在差异,则必须return "part numbers", "quantity" 并知道它属于 table 1 还是 table 2。 所以我创建了一个不存在的名为 "TypeTable" 的列,但它 return 是 NULL。

这是查询:

SELECT numParte
    ,Cantidad
    ,NULL AS "TypeTable"
FROM (
    SELECT numParte
        ,SUM(Cantidad) AS Cantidad
        ,"TypeTable" AS "Table1"
    FROM eboard.pye_hojadecarga
    WHERE id_chklistemb = 'IDHDC-1-HY'
    GROUP BY numParte

    UNION ALL

    SELECT numParte
        ,SUM(Cantidad) AS Cantidad
        ,"Table2"
    FROM eboard.pye_hojaconfirmacion
    WHERE id_hojadecarga = 'IDHDC-1-HY'
    GROUP BY numParte
    ) tbl
GROUP BY numParte
    ,Cantidad
HAVING count(*) = 1
ORDER BY numParte;

您的外部查询不正确,它将 NULL 指定为要显示的值。而是简单地显示第三列:

SELECT numParte
     , Cantidad
     , TypeTable
FROM (
    SELECT numParte
         , SUM(Cantidad) AS Cantidad
         , 'Table1'      AS TypeTable
    FROM eboard.pye_hojadecarga
    WHERE id_chklistemb = 'IDHDC-1-HY'
    GROUP BY numParte

    UNION ALL

    SELECT numParte
         , SUM(Cantidad) AS Cantidad
         ,'Table2'       AS TypeTable
    FROM eboard.pye_hojaconfirmacion
    WHERE id_hojadecarga = 'IDHDC-1-HY'
    GROUP BY numParte
    ) tbl
GROUP BY numParte
       , Cantidad
HAVING count(*) = 1
ORDER BY numParte;