Hello,
I have these two table on MySQL database version 8.0.12
(localhost)
general_table
----------------------------------------------------------
| N_rec | Related | unique_number | status_rec | pID |
----------------------------------------------------------
| 2023-0097 | Y | 98679 | Main | 97 |
| 2023-0201 | Y | 45311 | Lower | 201 |
----------------------------------------------------------
And
related_table
-------------------------------------------------------------------
| unique_number | N_rec_main | status_rec_main | N_rec | pID |
-------------------------------------------------------------------
| 45311 | 2023-0097 | Main | 2023-0201 | 201 |
-------------------------------------------------------------------
From the general table
it is possible to associate two rows that are similar.
In this example the N_rec
number 2023-0201
has been associated as a lower row of N_rec
number 2023-0097
, defined as main on the general table
.
Now I need insert into table related_table
this row:
----------------------------------------------------------
| N_rec | Related | unique_number | status_rec | pID |
----------------------------------------------------------
| 2023-0097 | Y | 98679 | Main | 97 |
----------------------------------------------------------
And update related_table
for this output:
------------------------------------------------------------------------
| unique_number | N_rec_main | status_rec | N_rec | pID |
------------------------------------------------------------------------
| 45311 | 2023-0097 | Main | 2023-0201 | 201 |
| 98679 | 2023-0201 | Lower | 2023-0097 | 97 |
------------------------------------------------------------------------
I have tried using
INSERT INTO `related_table` ( unique_number, N_rec, status_rec, N_rec ) SELECT
unique_number,
N_rec,
status_rec,
N_rec
FROM
`general_table`
WHERE
pID = 97;
But the output not what I expected
------------------------------------------------------------------------
| unique_number | N_rec_main | status_rec | N_rec | pID |
------------------------------------------------------------------------
| 98679 | 2023-0097 | Main | 2023-0097 | 97 |
| 45311 | 2023-0097 | Main | 2023-0201 | 201 |
------------------------------------------------------------------------
Any help?