How to insert comparison records results between 2 tables into another table?












1















status_tb



+----+----------+-------------+----------+
| id | status | description | state_id |
+----+----------+-------------+----------+
| 1 | new | north | 1 |
| 2 | assign | south | 2 |
| 3 |Postponed | east | 2 |
| 4 | Fixed | west | 3 |
| 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 |
| 7 | 35| Rejected | Testing | 4 |
+----------+----+----------+-------------+----------+


Once I get my input result from the resulting mysql, I need to insert those results to another table(audit_status_tb) like below...
How do I achieve it?



Here the sql to get my required records



(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)


audit_status_tb



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


Im not sure about what kind of insert-select? mysql I should used to retrieve those values and input them in the above format...










share|improve this question























  • Is new_id a primary key auto increment in audit_status_tb ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:35











  • yes, I just need to insert as from id

    – Emanula Sohn
    Nov 21 '18 at 12:36
















1















status_tb



+----+----------+-------------+----------+
| id | status | description | state_id |
+----+----------+-------------+----------+
| 1 | new | north | 1 |
| 2 | assign | south | 2 |
| 3 |Postponed | east | 2 |
| 4 | Fixed | west | 3 |
| 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 |
| 7 | 35| Rejected | Testing | 4 |
+----------+----+----------+-------------+----------+


Once I get my input result from the resulting mysql, I need to insert those results to another table(audit_status_tb) like below...
How do I achieve it?



Here the sql to get my required records



(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)


audit_status_tb



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


Im not sure about what kind of insert-select? mysql I should used to retrieve those values and input them in the above format...










share|improve this question























  • Is new_id a primary key auto increment in audit_status_tb ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:35











  • yes, I just need to insert as from id

    – Emanula Sohn
    Nov 21 '18 at 12:36














1












1








1








status_tb



+----+----------+-------------+----------+
| id | status | description | state_id |
+----+----------+-------------+----------+
| 1 | new | north | 1 |
| 2 | assign | south | 2 |
| 3 |Postponed | east | 2 |
| 4 | Fixed | west | 3 |
| 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 |
| 7 | 35| Rejected | Testing | 4 |
+----------+----+----------+-------------+----------+


Once I get my input result from the resulting mysql, I need to insert those results to another table(audit_status_tb) like below...
How do I achieve it?



Here the sql to get my required records



(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)


audit_status_tb



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


Im not sure about what kind of insert-select? mysql I should used to retrieve those values and input them in the above format...










share|improve this question














status_tb



+----+----------+-------------+----------+
| id | status | description | state_id |
+----+----------+-------------+----------+
| 1 | new | north | 1 |
| 2 | assign | south | 2 |
| 3 |Postponed | east | 2 |
| 4 | Fixed | west | 3 |
| 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 |
| 7 | 35| Rejected | Testing | 4 |
+----------+----+----------+-------------+----------+


Once I get my input result from the resulting mysql, I need to insert those results to another table(audit_status_tb) like below...
How do I achieve it?



Here the sql to get my required records



(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)


audit_status_tb



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


Im not sure about what kind of insert-select? mysql I should used to retrieve those values and input them in the above format...







mysql database insert compare






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 12:31









Emanula SohnEmanula Sohn

275




275













  • Is new_id a primary key auto increment in audit_status_tb ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:35











  • yes, I just need to insert as from id

    – Emanula Sohn
    Nov 21 '18 at 12:36



















  • Is new_id a primary key auto increment in audit_status_tb ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:35











  • yes, I just need to insert as from id

    – Emanula Sohn
    Nov 21 '18 at 12:36

















Is new_id a primary key auto increment in audit_status_tb ?

– Madhur Bhaiya
Nov 21 '18 at 12:35





Is new_id a primary key auto increment in audit_status_tb ?

– Madhur Bhaiya
Nov 21 '18 at 12:35













yes, I just need to insert as from id

– Emanula Sohn
Nov 21 '18 at 12:36





yes, I just need to insert as from id

– Emanula Sohn
Nov 21 '18 at 12:36












1 Answer
1






active

oldest

votes


















1














You can simply use Insert Into .. Select statement:



INSERT INTO audit_status_tb (id, Column_changed, Old_value, New_value) 

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





share|improve this answer
























  • thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

    – Emanula Sohn
    Nov 21 '18 at 12:39











  • @EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:43











  • Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

    – Emanula Sohn
    Nov 21 '18 at 17:59













  • @EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

    – Madhur Bhaiya
    Nov 21 '18 at 18:01











  • how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

    – Emanula Sohn
    Nov 21 '18 at 18:12













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412101%2fhow-to-insert-comparison-records-results-between-2-tables-into-another-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can simply use Insert Into .. Select statement:



INSERT INTO audit_status_tb (id, Column_changed, Old_value, New_value) 

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





share|improve this answer
























  • thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

    – Emanula Sohn
    Nov 21 '18 at 12:39











  • @EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:43











  • Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

    – Emanula Sohn
    Nov 21 '18 at 17:59













  • @EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

    – Madhur Bhaiya
    Nov 21 '18 at 18:01











  • how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

    – Emanula Sohn
    Nov 21 '18 at 18:12


















1














You can simply use Insert Into .. Select statement:



INSERT INTO audit_status_tb (id, Column_changed, Old_value, New_value) 

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





share|improve this answer
























  • thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

    – Emanula Sohn
    Nov 21 '18 at 12:39











  • @EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:43











  • Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

    – Emanula Sohn
    Nov 21 '18 at 17:59













  • @EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

    – Madhur Bhaiya
    Nov 21 '18 at 18:01











  • how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

    – Emanula Sohn
    Nov 21 '18 at 18:12
















1












1








1







You can simply use Insert Into .. Select statement:



INSERT INTO audit_status_tb (id, Column_changed, Old_value, New_value) 

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





share|improve this answer













You can simply use Insert Into .. Select statement:



INSERT INTO audit_status_tb (id, Column_changed, Old_value, New_value) 

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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 12:36









Madhur BhaiyaMadhur Bhaiya

19.6k62236




19.6k62236













  • thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

    – Emanula Sohn
    Nov 21 '18 at 12:39











  • @EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:43











  • Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

    – Emanula Sohn
    Nov 21 '18 at 17:59













  • @EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

    – Madhur Bhaiya
    Nov 21 '18 at 18:01











  • how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

    – Emanula Sohn
    Nov 21 '18 at 18:12





















  • thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

    – Emanula Sohn
    Nov 21 '18 at 12:39











  • @EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

    – Madhur Bhaiya
    Nov 21 '18 at 12:43











  • Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

    – Emanula Sohn
    Nov 21 '18 at 17:59













  • @EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

    – Madhur Bhaiya
    Nov 21 '18 at 18:01











  • how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

    – Emanula Sohn
    Nov 21 '18 at 18:12



















thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

– Emanula Sohn
Nov 21 '18 at 12:39





thanks that what I try yesterday but wasnt sure where to properly put the select statement for the insert

– Emanula Sohn
Nov 21 '18 at 12:39













@EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

– Madhur Bhaiya
Nov 21 '18 at 12:43





@EmanulaSohn my query should work. Let me know if you are facing any syntax errors ?

– Madhur Bhaiya
Nov 21 '18 at 12:43













Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

– Emanula Sohn
Nov 21 '18 at 17:59







Error, Im getting duplicate if I re-execute the query. I should not have any duplicate results in the audit_status_tb table.Help pls By duplicate I mean, same id, state_id, status , description rows in some cases...

– Emanula Sohn
Nov 21 '18 at 17:59















@EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

– Madhur Bhaiya
Nov 21 '18 at 18:01





@EmanulaSohn it will obviously reenter the same data. One way is to put a composite unique constraint on (id, Column_changed, old_value, new_value)

– Madhur Bhaiya
Nov 21 '18 at 18:01













how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

– Emanula Sohn
Nov 21 '18 at 18:12







how do I add this composite unique constraint? I try adding WHERE s.id NOT IN (SELECT id FROM audit_status_tb) at the end of each select statement and its seems to work...What do you think?

– Emanula Sohn
Nov 21 '18 at 18:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412101%2fhow-to-insert-comparison-records-results-between-2-tables-into-another-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?