gpt4 book ai didi

Mysql查询显示两个表中不匹配的记录,如旧记录和新记录

转载 作者:行者123 更新时间:2023-11-29 16:20:28 25 4
gpt4 key购买 nike

我有两个表,一个是临时表,另一个是主表。在进行最终处理之前,用户单击刷新按钮。然后,我将数据填充到临时表中,并且需要向用户显示两个表之间是否存在任何差异(例如旧值 - 新值)。为此,我尝试了下面的查询,但无法获取旧值。我得到的是 null

Temporay table

+------+--------------+----------------+-----+
| asn | ean | belegnr |quantity|
+------+--------------+----------------+-----+
| 700000845 | 4046228201122| 30059314 |2 |
| 700000845 | 4046228138879| 30059314 |3 |
| 700000845 | 4046228138875| 30059320 |25 |
+------+--------------+----------------+--+--+

主表

+------+--------------+----------------+-----+
| asn | ean | belegnr |quantity|
+------+--------------+----------------+-----+
| 700000845 | 4046228201122| 30059314 |5 |
| 700000845 | 4046228138879| 30059314 |3 |
| 700000845 | 4046228138888| 30059320 |25 |
+------+--------------+----------------+--+--+

从这两个表中,我的 ean 字段和数量字段都有变化为此,我使用此查询

SELECT
t2.id,t2.ean,t2.belegnr,t2.`quantity`,t1.`quantity` FROM
temp_table t2
LEFT OUTER JOIN details t1 ON
t1.asn = t2.asn
AND t1.ean = t2.ean
AND t1.belegnr = t2.belegnr
AND t1.quantity = t2.quantity
WHERE t2.asn = 700000845
AND t2.`belegnr` in(30059314,30059320)
AND t1.id IS NULL

下面是我的预期结果

+------+--------------+----------------+-----+---------------------- 
| asn | ean | oldean| belegnr |quantity|Oldqty|
+------+--------------+----------------+-----+----------------------|
| 700000845 | 4046228201122| 4046228201122 |30059314 |2 | 5 |
| 700000845 | 4046228138875| 4046228138888 |30059320 |25 | 25 |
+------+--------------+----------------+--+--+--------+-------------+

任何帮助将不胜感激。

最佳答案

一旦您加入,您就消除了您想要称为“旧”的 t1 行。

假设 asn 不变:

SELECT
t2.asn,t2.ean,t1.ean as oldean,t2.belegnr,t1.belegnr as oldbelegnr, t2.`quantity`,t1.`quantity` as Oldqty
FROM temp_table t2
JOIN details t1 ON t1.asn = t2.asn
WHERE
t1.ean <> t2.ean
OR t1.belegnr <> t2.belegnr
OR t1.quantity <> t2.quantity

关于Mysql查询显示两个表中不匹配的记录,如旧记录和新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54545690/

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