Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have two tables:

A [ID, column1, column2, column3]

B [ID, column1, column2, column3, column4]

A will always be subset of B (meaning all columns of A are also in B).

I want to update a record with a specific ID in B with their data from A for all columns of A. This ID exists both in A and B.

Is there an UPDATE syntax or any other way to do that without specifying the column names, just saying "set all columns of A"?

I'm using PostgreSQL, so a specific non-standard command is also accepted (however, not preferred).

1 Answer

0 votes
by (40.7k points)

update q

set q.QuestionID = a.QuestionID

from QuestionTrackings q

inner join QuestionAnswers a

on q.AnswerID = a.AnswerID

where q.QuestionID is null -- and other conditions you might want

I would suggest you to check what the result set to update is before running the update (same query, just with a select):

select *

from QuestionTrackings q

inner join QuestionAnswers a

on q.AnswerID = a.AnswerID

where q.QuestionID is null -- and other conditions you might want

Particularly whether each answer id has definitely only 1 associated question id.

Related questions

Browse Categories

...