473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple Update Statement

Jezternz
145 New Member
Expand|Select|Wrap|Line Numbers
  1. UPDATE items i, cartitems c 
  2. SET stock = (stock-1)
  3. WHERE i.id = c.itemid AND c.userid = '7';
  4.  
Okay so I have a table for 'items' and a table for 'items in a users shopping cart'. I want to decrement the stock field in the items table once for every instance of that same item in the 'items in shopping cart' table.

The code above, will unfortunately only decrement a maximum of once in stock per different item.

Any help would be greatly appreciated, and please don't suggest to add quantity fields to the 'items in shopping cart' table as this not an option.

Cheers, J
Oct 15 '10 #1
2 1804
Atli
5,058 Recognized Expert Expert
Hey.

You could use a dependent subquery. Basically, fetching the number of rows in the "items in shopping cart" table that match the ID of the item for each item and subtracting it from the existing number.

For example:
Expand|Select|Wrap|Line Numbers
  1. UPDATE items AS i
  2. SET
  3.     i.stock = i.stock - (
  4.         SELECT     COUNT(*)
  5.         FROM     user_items AS ui
  6.         WHERE     ui.item_id = i.id
  7.     );
Keep in mind that dependent subqueries can be resource hogs, but I assume this is going to be more of a rare maintenance thing, not something that is executed all the time?


You may also want to look into adding a Trigger to the "user_items" table, so that every time a row is added into it the items table is updated automatically.
Oct 15 '10 #2
Jezternz
145 New Member
Thanks for the quick response Atli,

As this is more for a trial then actual practical use, efficiency is not really a concern.
I would however be interested in an efficient one. If you have any links or anything that would help with writing a more efficient one, that would be great.

Cheers, J
Oct 15 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Clive Foley | last post by:
Hey all, i have a students_table which i want to do a multiple update on. Update student_table set grant = 35000 where course_id = 2 There are lots of students in the student table which...
2
by: MAS | last post by:
Below is a simple UPDATE that I have to perform on a table that has about 2.5 million rows (about 4 million in production) This query runs for an enourmous amount of time (over 1 hour). Both the...
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
5
by: Wing | last post by:
Hi all, I am writing a function that can change the value "Quantity" in the selected row of MS SQL table "shoppingCart", my code is showing below ...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
2
by: travhale | last post by:
in a new project using .net 2005, c#. getting err message "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." source RDBMS is oracle 8i. I add a new...
3
by: somesteve | last post by:
I feel so stupid for posting this but I cannot figure out why I'm getting an error with this simple update query. update set = '02/14/06 06:00:00 AM' where = '02/14/20 06:00:00 AM' Gives...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.