Skip to Main Content

MySQL Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Update statement is not updating the right values

Gayathri VenugopalMar 30 2019 — edited Mar 30 2019

Hi, I need to update UnitinStock and Shipped Date of products based on these conditions.

1)When the shipped date is null and

2) when Quantity < Unit in stock.

But my query is updating Shipped date of all the orders even when this condition Quantity < Unit in stock is not satisfied  .

How do I satisfy these 2 conditions and update colums:

Update products,orderdetails,orders

set products.UnitsInStock = (products.UnitsInStock - orderdetails.Quantity),  ShippedDate =    current_date()

where products.ProductID =orderdetails .ProductID and orders.OrderID = orderdetails.OrderID

and (ShippedDate is null and orderdetails.Quantity < UnitsInStock) and  orders.OrderID  = 11039

Comments

Gaz in Oz

Your update syntax suggests you are working with MYSQL db.

It always helps to format your code into a more readable and maintainable style, for example:

Update products,

       orderdetails,

       orders

set    products.UnitsInStock = (products.UnitsInStock - orderdetails.Quantity),

       ShippedDate = current_date()

where  products.ProductID = orderdetails.ProductID

and    orders.OrderID = orderdetails.OrderID

and   (ShippedDate is null and

       orderdetails.Quantity < UnitsInStock)

and    orders.OrderID  = 11039;

The fact the update is updating rows you don't want it to means you haven't got the join to the three tables correct.

Write it as a select statement first then convert it into an update when you are sure it will update the rows you want it to.

...and as this looks like a mysql question you be better off asking your question here or

here http://forums.mysql.com/

John Thorton

Gayathri Venugopal wrote:

Hi, I need to update UnitinStock and Shipped Date of products based on these conditions.

1)When the shipped date is null and

2) when Quantity < Unit in stock.

But my query is updating Shipped date of all the orders even when this condition Quantity < Unit in stock is not satisfied .

How do I satisfy these 2 conditions and update colums:

Update products,orderdetails,orders

set products.UnitsInStock = (products.UnitsInStock - orderdetails.Quantity), ShippedDate = current_date()

where products.ProductID =orderdetails .ProductID and orders.OrderID = orderdetails.OrderID

and (ShippedDate is null and orderdetails.Quantity < UnitsInStock) and orders.OrderID = 11039

Please click on URL below & provide details as stated in #5 - #9 inclusive

How do I ask a question on the forums?

Please post full results from SQL below

SELECT * FROM V$VERSION;

mathguy

You were told two days ago, in a different thread you started, that this is NOT A MySQL FORUM.   Repeat:  This is NOT A MySQL FORUM.  Is that hard to understand?

Timo Hahn

***Moderator action (Timo): User, I moved you thread to the MySql space as it isn't about OracleDB.***

1 - 4

Post Details

Added on Mar 30 2019
4 comments
399 views