Как получить разницу значений сравнения столбцов между двумя таблицами в MySQL - PullRequest
0 голосов
/ 20 ноября 2018

status_tb

+----+----------+-------------+----------+
| id | status   | description | state_id |
+----+----------+-------------+----------+
|  1 |     new  | north       | 1        |
|  2 |   assign | south       | 2        |
|  3 |Postponed | east        | 2        |
|  4 |    Fixed | west        | 3        |
|  8 | Verified | north-east  | 1        |
|  9 |   Closed | south-west  | 2        |
| 35 |     Test | South-test  | 4        |
+----+----------+-------------+----------+

status_backup_tb

+----------+----+----------+-------------+----------+
|backup_id | id | status   | description | state_id |
+----------+----+----------+-------------+----------+
|  1       |  1 |new       | north       | 1        |
|  2       |  2 |assign    | south       | 2        |
|  3       |  3 |Postponed | east        | 2        |
|  4       |  4 | Fixed    | west        | 3        |
|  5       |  8 | Verified | north-east  | 1        |
|  6       |  9 | Closed   | south-west  | 2        |
|  7       |  35| Rejected | Testing     | 4        |
+----------+----+----------+-------------+----------+

Требуемый результат: Только столбец_измененный, old_value и new_value

 |new_id | id |Column_changed| Old_value   |New_value |
 +-------+----+--------------+-------------+----------+
 |1      | 35 | status       | Test        | Rejected |
 |2      | 35 |description   | South-test  | Testing  |  
 +-------+----+--------------+-------------+----------+

Если state_id и status был тем, кто изменился, я хотел получить столбец id, status and state_id и их old_values ​​и new_values ​​вместо этого.

Уже пробовал использовать это, но не сработало

SELECT MIN(TableName) as TableName, ID, COL1, COL2, COL3 ...
FROM
(
 SELECT 'Table A' as TableName, A.ID, A.COL1, A.COL2, A.COL3, ...
 FROM A
 UNION ALL
 SELECT 'Table B' as TableName, B.ID, B.COL1, B.COl2, B.COL3, ...
 FROM B
 ) tmp
GROUP BY ID, COL1, COL2, COL3 ...
HAVING COUNT(*) = 1
ORDER BY ID

Ответы [ 2 ]

0 голосов
/ 20 ноября 2018

Попробуйте ниже:

((select a.id, 'status' as Column_changed, a.status, b.status
From status_tb as a inner join status_backup_tb as b on a.id = b.id
Where a.status <> b.status)
UNION
(select a.id, 'description' as Column_changed, a.description, b.description
From status_tb as a inner join status_backup_tb as b on a.id = b.id
Where a.status <> b.status))
UNION
((select a.id, 'status' as Column_changed, a.status, b.status
From status_tb as a inner join status_backup_tb as b on a.id = b.id
Where a.status_id <> b.status_id)
UNION
(select a.id, 'description' as Column_changed, a.description, b.description
From status_tb as a inner join status_backup_tb as b on a.id = b.id
Where a.status_id <> b.status_id))
0 голосов
/ 20 ноября 2018

Один из способов - получить все изменения для соответствующих полей в отдельных Select запросах.В конечном итоге Union результаты этих нескольких запросов.

Мы JOIN между двумя таблицами используем id и условие, что соответствующие значения столбцов не совпадают.

(SELECT 
   s.id, 
   'status' AS Column_changed, 
   s.status AS Old_value, 
   b.status AS New_value 
 FROM status_tb AS s
 JOIN status_backup_tb AS b 
   ON b.id = s.id AND 
      b.status <> s.status)

UNION ALL 

(SELECT 
   s.id, 
   'description' AS Column_changed, 
   s.description AS Old_value, 
   b.description AS New_value 
 FROM status_tb AS s
 JOIN status_backup_tb AS b 
   ON b.id = s.id AND 
      b.description <> s.description)

UNION ALL

(SELECT 
   s.id, 
   'state_id' AS Column_changed, 
   s.state_id AS Old_value, 
   b.state_id AS New_value 
 FROM status_tb AS s
 JOIN status_backup_tb AS b 
   ON b.id = s.id AND 
      b.state_id <> s.state_id)

ORDER BY id
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...