Under Update MySQL

Asked 2 months ago, Updated 2 months ago, 1 views

If you have the following data:
If the parent id is the same and the amount is NULL, you can rewrite it to something that is not NULL. What should I do?
I look forward to hearing from you.
From this state
Parent id, Child id, Amount
A001,001,100
A001,002,NULL
A002,001,NULL
A002,002,200
A002,003,NULL

Parent id, Child id, Amount
A001,001,100
A001,002,100
A002,001,200
A002,002,200
A002,003,200
I want to bring it to

mysql

2022-09-30 14:20

1 Answers

If there is only one record with a non-NULL amount for each parent id, you can do the following:
Replace the table name and column name with the appropriate one.

UPDATE `table name `table_a, `table name `table_b
SET table_a. Amount = table_b. Amount
WHERE table_a. Parent id = table_b. Parent id
AND table_a. Amount IS NULL
AND table_b. Amount IS NOT NULL;


2022-09-30 14:20

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.