1

I have a table with some null values in column "date":

platform date id
---------------------------
web 2018年10月10日 1
mob 1
mob 1
web 2018年10月15日 2
mob 2
ntl 2018年10月09日 3
web 2018年10月12日 3
web 2018年10月11日 4
mob 3

I want to update null values in 'date' for 'mob' platform by matching the 'id' column from platform 'web'. The result should look like this:

platform date id
---------------------------
web 2018年10月10日 1
mob 2018年10月10日 1
mob 2018年10月10日 1
web 2018年10月15日 2
mob 2018年10月15日 2
ntl 2018年10月09日 3
web 2018年10月12日 3
web 2018年10月11日 4
mob 2018年10月12日 3

Will really appreciate your help!

Gonçalo Peres
1451 gold badge1 silver badge8 bronze badges
asked Oct 18, 2018 at 23:21
0

1 Answer 1

1
UPDATE `table` t1, `table` t2
SET t1.`date` = t2.`date`
WHERE t1.id = t2.id
 AND t1.platform = 'mob'
 AND t2.platform = 'web'
 AND t1.`date` IS NULL
 AND t2.`date` IS NOT NULL

PS. The solution assumes that in a subarray of the table which matched platform = 'web' AND t2.`date` IS NOT NULL condition the id value is unique. If not, a random value from possible values list will be used, and you must use a subquery instead of t2 table copy, which filters data by platform = 'web' condition, groups it by id and selects alone data value (maximal, for example) for each id value. If your MySQL version is 8+, you may do it in WITH clause.

answered Oct 19, 2018 at 5:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.