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

时间:2018-12-19 13:14:09

标签: mysql comparison

我在StackOverflow上浏览了各种主题,但找不到适合我问题的正确解决方案。这是我所拥有的:

我有两个数据库表。一个表应该是某种参考模型,看起来类似于此示例:

if ($request->hasFile('image')) {
    dd('write code here');
}

//or 

if ($request->file('image')->isValid()) {
    dd('write code here');
}

//There are few ways:
if (!empty($location->file))
if (!products->isEmpty())
if (count($products) > 0)
if ($products->count() > 0)

//flash messages in the session
Session::flash('message', 'This is a message!'); 
Session::flash('alert-class', 'alert-danger'); 

@if(Session::has('message'))
p class="alert {{ Session::get('alert-class', 'alert-info') }}"{{ Session::get('message') }}
@endif

现在我将新数据输入第二个表。这些数据可以具有相同的值,也可以具有相同的行数。但是也可能发生,某些数据有更多行,或者行内的值不同。这是另一个表格示例,其中我多排两个值不同

+----+----------------+----------------+----------------+-----------------+
| 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  |
+----+----------------+----------------+----------------+-----------------+

我正在寻找比较这两个表并列出所有不同记录的SQL语句。在上面的示例中,SQL语句应重新浏览这些信息:

+----+----------------+-----------------+-----------------+----------------+
| 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 |
+----+----------------+-----------------+-----------------+----------------+

这是我到目前为止尝试过的:

+----+----------------+-----------------+-----------------+----------------+
| 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

该代码仅返回第6行,但不会向我显示ID 3和5中的新值。

任何帮助将不胜感激。预先感谢!

编辑: 根据@Madhur Bhaiya解决方案,我犯了一个错误。输出应如下所示:

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

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

2 个答案:

答案 0 :(得分:2)

我们可以使用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 |

View on DB Fiddle

答案 1 :(得分:0)

我相信您正在寻找的不是相交条款,而是相交条款。 Union将一次报告两个表中的所有内容(根据distict修饰符)。相交只会报告共同点。