0

I am unable to update 2 fields in a Table using UDPATE AND IF condition. Need help.

I have an invoice table where in If field Nos=0 I need to update field Qty from Capacity and field Nos=1 in the same table.

My sql statement is not working:

UPDATE INVDTLS_draft1 SET `Nos`=1, `Qty`=`Capacity` IF (Nos=0) WHERE id=id

error message:

#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 'IF (Nos=0) WHERE id=id' at line 1

3 Answers 3

1

try this query, using AND operator

UPDATE INVDTLS_draft1 SET `Nos`=1, `Qty`=`Capacity` WHERE id=id AND Nos=0
Sign up to request clarification or add additional context in comments.

Comments

0

Why id=id? I think you need this:

UPDATE INVDTLS_draft1
SET    `Nos`=1, `Qty`=`Capacity`
WHERE  Nos=0

1 Comment

Yes you are right, it is a simple statement, not sure why I was thinking about IF condidtion to do this. Thankyou very much.
0

Try this:

UPDATE INVDTLS_draft1 SET 
Nos=1,
Qty = if(nos=0, capacity, Qty)
WHERE id=?

This "does nothing" to qty if nos != 0.

2 Comments

This statement is updating all the records in the table, it is not checking for condition
@user1114409 I just copied the where clause from the question. I assumed everyone knew OP meant an app code variable named "id". I've edited the answer to show a placeholder instead

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.