比较两个数据库表的值->mysql

smtd7mpg  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(433)

我在这里浏览了关于stackoverflow的各种主题,但没有找到解决问题的正确方法。这是我想要的´我们有:
我有两个数据库表。一个表应该是某种参考模型,类似于此示例:

+----+----------------+----------------+----------------+-----------------+
| ID |     FIELD1     |     FIELD2     |     FIELD3     |     FIELD 4     |
+----+----------------+----------------+----------------+-----------------+
|  1 | Value1_Field_1 | Value1_Field_2 | Value1_Field_3 | Value1_Field_4  |
|  2 | Value2_Field_1 | Value2_Field_2 | Value2_Field_3 | Value2_Field_4  |
|  3 | Value3_Field_1 | Value3_Field_2 | Value3_Field_3 | Value3_Field_4  |
|  4 | Value4_Field_1 | Value4_Field_2 | Value4_Field_3 | Value4_Field_4  |
|  5 | Value5_Field_1 | Value5_Field_2 | Value5_Field_3 | Value5_Field_4  |
+----+----------------+----------------+----------------+-----------------+

现在我向第二个表输入新数据。这些数据可以具有相同的值,也可以具有相同的行数。但也有可能出现这样的情况:一些数据有更多的行,或者行中的值不同。下面是另一个表格示例,其中我有一行多,两个值不同:

+----+----------------+-----------------+-----------------+----------------+
| ID |     FIELD1     |     FIELD2      |     FIELD3      |    FIELD 4     |
+----+----------------+-----------------+-----------------+----------------+
|  1 | Value1_Field_1 | Value1_Field_2  | Value1_Field_3  | Value1_Field_4 |
|  2 | Value2_Field_1 | Value2_Field_2  | Value2_Field_3  | Value2_Field_4 |
|  3 | Value3_Field_1 | Value3_Field_2  | Value3_NEWVALUE | Value3_Field_4 |
|  4 | Value4_Field_1 | Value4_Field_2  | Value4_Field_3  | Value4_Field_4 |
|  5 | Value5_Field_1 | Value5_NEWVALUE | Value5_Field_3  | Value5_Field_4 |
|  6 | Value6_Field_1 | Value6_Field_2  | Value6_Field_3  | Value6_Field_4 |
+----+----------------+-----------------+-----------------+----------------+

我正在寻找一个sql语句,比较这两个表,并列出所有不同的记录。在我上面的示例中,sql语句应该返回这些信息:

+----+----------------+-----------------+-----------------+----------------+
| ID |     FIELD1     |     FIELD2      |     FIELD3      |    FIELD 4     |
+----+----------------+-----------------+-----------------+----------------+
|  3 |                |                 | Value3_NEWVALUE |                |
|  5 |                | Value5_NEWVALUE |                 |                |
|  6 | Value6_Field_1 | Value6_Field_2  | Value6_Field_3  | Value6_Field_4 |
+----+----------------+-----------------+-----------------+----------------+

这是我想要的´到目前为止,我已经尝试过:

SELECT distinct FIELD1, FIELD2, FIELD3, FIELD4 from table_references
union
SELECT distinct FIELD1, FIELD2, FIELD3, FIELD4  from table_new_data

该语句返回所有行,但只返回一次。这不是我要找的。我也试过这个代码:

SELECT FIELD1, FIELD2, FIELD3, FIELD4
FROM (
SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM table_references
UNION ALL
SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM table_new_data
) tbl
GROUP BY FIELD1
HAVING count(*) = 1
ORDER BY FIELD1

该代码只返回第6行,但没有显示id 3和5中的新值。
任何帮助都将不胜感激。提前谢谢!
编辑:根据@madhur bhaiya解决方案,我犯了一个错误。输出应如下所示:

+----+----------------+-----------------+-----------------+----------------+
| ID |     FIELD1     |     FIELD2      |     FIELD3      |    FIELD 4     |
+----+----------------+-----------------+-----------------+----------------+
|  3 | Value3_Field1  | Value3_Field2   | Value3_NEWVALUE | Value3_Field4  |
|  5 | Value5_Field1  | Value5_NEWVALUE | Value5_Field3   | Value5_Field4  |
|  6 | Value6_Field_1 | Value6_Field_2  | Value6_Field_3  | Value6_Field_4 |
+----+----------------+-----------------+-----------------+----------------+

所以我还需要一个值不同的受影响行中的所有值。

0dxa2lsx

0dxa2lsx1#

我们可以用 LEFT JOIN 比较两个表,然后使用条件函数,如 If() 要获得相应的值:
查询

SELECT tnew.*
FROM table_new_data AS tnew
LEFT JOIN table_references AS told
  ON told.ID = tnew.ID
WHERE (told.ID IS NOT NULL AND 
       (tnew.FIELD1 <> told.FIELD1 OR 
        tnew.FIELD2 <> told.FIELD2 OR 
        tnew.FIELD3 <> told.FIELD3 OR 
        tnew.FIELD4 <> told.FIELD4)
      ) OR 
      told.ID IS NULL;

结果

| ID  | FIELD1         | FIELD2          | FIELD3          | FIELD4         |
| --- | -------------- | --------------- | --------------- | -------------- |
| 3   | Value3_Field_1 | Value3_Field_2  | Value3_NEWVALUE | Value3_Field_4 |
| 5   | Value5_Field_1 | Value5_NEWVALUE | Value5_Field_3  | Value5_Field_4 |
| 6   | Value6_Field_1 | Value6_Field_2  | Value6_Field_3  | Value6_Field_4 |

db fiddle视图

xe55xuns

xe55xuns2#

我相信你要找的不是联合条款,而是intersect条款。union将报告两个表中的所有内容,一次(按distict修饰符)。intersect只报告共性。

相关问题