问题
Could anyone tell me what is wrong with this and why I am getting the error #1248 - Every derived table must have its own alias. ?
UPDATE (SELECT t.crime_definition p1, s.crime_definition p2
FROM crime_2007 t, crime_2010 s
WHERE crime_2007.crime_id_2007 = crime_2010.crime_id)
SET p1 = p2
I have researched for a long time and I can't pin point the problem. I am trying to copy a column from one table to a column in a different table.
thank you in advance!
回答1:
The "derived" table is the SELECT between parenthesis. You can (and should in your case) give it a (short-lived) name.
UPDATE ( SELECT t.crime_definition p1, s.crime_definition p2
FROM crime_2007 t, crime_2010 s
WHERE crime_2007.crime_id_2007 = crime_2010.crime_id
) AS dummyName
SET p1 = p2
But the above will not work either (at least in MYSQL) as it will tell you that "The target table dummyName is not updateable".
You can use this which should work in many systems as it's standard ANSI SQL:
UPDATE crime_2007
SET crime_definition =
( SELECT s.crime_definition
FROM crime_2010 s
WHERE crime_2007.crime_id_2007 = s.crime_id
)
Many systems use "non-standard" SQL, like Lamak's answer for SQL-Server and this that will work in MYSQL only:
UPDATE crime_2007 t, crime_2010 s
SET t.crime_definition = s.crime_definition
WHERE t.crime_id_2007 = s.crime_id
回答2:
You should try changing the syntax of your UPDATE
(I'm also changing your implicit JOIN
for an explicit one):
UPDATE crime_2007
SET t.crime_definition = s.crime_definition
FROM crime_2007 t
INNER JOIN crime_2010 s
WHERE t.crime_id_2007 = s.crime_id
The above works for SQL Server, but since you are probably using MySql, you should go with @ypercube 's answer
来源:https://stackoverflow.com/questions/5467801/i-am-getting-a-1248-every-derived-table-must-have-its-own-alias-i-am-using-a