2015-10-18 71 views
0

我正在使用以下格式在同一查詢中使用selectupdate命令。在mysql查詢中更新並選擇查詢

UPDATE t 
    SET t.col1 = o.col1 
    FROM table1 AS t 
     INNER JOIN 
     table2 AS o 
     ON t.id = o.id 

我在我的查詢中應用了相同的概念,但它引發了一個我無法解決的錯誤。任何想法,我哪裏錯了?

update T set T.price = 2*OT.ingredients from Cake as T Inner join (select 
    A.cakeid, B.price, sum(C.price) ingredients 
from 
    Contain as A 
     inner join 
    Cake as B ON A.cakeid = B.cakeid 
     inner join 
    Ingredient as C ON C.ingredid = A.ingredid 
group by A.cakeid 
having B.price <= 2 * sum(C.price)) as OT on OT.cakeid = T.cakeid 

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from Cake as T Inner join (select A.cakeid, B.price, sum(C.price) ingredien' at line 1 
+2

第一個查詢是不爲MySQL正確的語法。 –

回答

1

在MySQL正確的語法是:

update Cake T Inner join 
     (select A.cakeid, B.price, sum(C.price) ingredients 
     from Contain A inner join 
      Cake B 
      ON A.cakeid = B.cakeid inner join 
      Ingredient as C 
      ON C.ingredid = A.ingredid 
     group by A.cakeid 
     having B.price <= 2 * sum(C.price) 
     ) OT 
     on OT.cakeid = T.cakeid 
    set T.price = 2*OT.ingredients ;