I have two tables with same structure. One table contains old value and another table contains new value of all columns. I want to get out out as columns as 1 column and old and new value row wise with changed value of columns.
CREATE TABLE test (
Column1 varchar(100) NULL,
Column2 varchar(100) NULL,
Column3 varchar(100) NULL
);
INSERT INTO webtest.test
(Column1, Column2, Column3)
VALUES('1', '2', '3');
CREATE TABLE test_hist (
Column1 varchar(100) NULL,
Column2 varchar(100) NULL,
Column3 varchar(100) NULL
);
INSERT INTO test
(Column1, Column2, Column3)
VALUES('1', '3', '4');
I want output like below:
| Field_Name | Old_Value| | New_Value|
| -------- | -------- |
| Column2 | 2 | 4
| Column3 | 3 | 5
any help would be highly appreciated. thanks in advance.
I tried to use pivot but that is not available in mysql.
2
Answers
You can use a combination of
subqueries
andUNION
.How about union of joins?
For sample data you posted, result is
Have a look at the fiddle that illustrates it.