0

I am using the below format to use select and update command in the same query.

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

I applied the same concept in my query but it is throwing an error which I am not able resolve. Any idea where I am going wrong here?

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
1
  • 2
    The first query is not correct syntax for MySQL. Commented Oct 18, 2015 at 16:13

1 Answer 1

1

The correct syntax in MySQL is:

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 ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.