PROBLEM:
I have two tables, animal table and placeholder table. I want to do an UPDATE
on the animal table so that the appropriate blanks get filled in. The animal
table is not properly normalized.
Is it possible to do this with pure MySQL SQL statements and not resorting to
looping inside a program or script?
EXAMPLE:
== Here is the “before” ==
animal placeholder
1 | dog 1 | dog | log
1 | snog 2 | cat | mat
1 | _blank_ 3 | bird | word
2 | cat
2 | sat
2 | _blank_
3 | bird
3 | heard
3 | _blank_
== Here is the “after” ==
animal placeholder
1 | dog 1 | dog | log
1 | snog 2 | cat | mat
1 | log 3 | bird | word
2 | cat
2 | sat
2 | mat
3 | bird
3 | heard
3 | word
QUESTION:
Can you construct a MySQL SQL statement that will use UPDATE to transform
“before” into “after”??
I am assuming it will be necessary to use JOIN since the query needs to know the correct place to put the placeholders.
== Solution ==
UPDATE animal a
SET name = (
SELECT noun
FROM placeholder
WHERE (
a.id = id
)
) WHERE a.name is null;
maybe something similar to this :