Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

database - Sqlite - Update table rows based on another data in the row

I'm looking for the way how to update the empty values, based on the similar value from another row:

Pic1

As you can see from the image: rowid with a number (331) contains the website value (tbe.com), but two another rows marked as '?'. I want to sync the data if some of the rows marked '?' for equal company names.

Thanks!

question from:https://stackoverflow.com/questions/65837473/sqlite-update-table-rows-based-on-another-data-in-the-row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If actually there are NULLs in the column website instead of ? then

UPDATE tablename AS t1
SET website = (SELECT MAX(t2.website) FROM tablename t2 WHERE t2.company = t1.company)
WHERE t1.website IS NULL

If there are ?:

UPDATE tablename AS t1
SET website = COALESCE((
  SELECT MAX(t2.website) 
  FROM tablename t2 
  WHERE t2.company = t1.company AND t2.website <> t1.website
), t1.website)
WHERE t1.website = '?'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...