I am getting a #1248 - Every derived table must have its own alias. I am using a WAMP server

感情迁移 提交于 2020-01-06 16:51:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!