By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

sql query update

P: n/a
Hi

i need help with formulating a query that will update the a field on one
table depending on the values from another for example

i have a cart table: cartid, buyerid, productid, quantity

i have a product table: productid, quantity

i want to do a:

select * from cart where buyerid=x

and then depending on the returned rows i want to update the product table:

update product set quantity = quantity - cart.quantity where
productid=cart.productid

if you see what i mean?

Any help appreciated

Kal
Nov 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
UPDATE product
SET quantity = quantity -
(SELECT SUM(quantity)
FROM cart
WHERE cart.buyerid = x
AND cart.productid = product.productid)
WHERE EXISTS
(SELECT *
FROM cart
WHERE cart.buyerid = x
AND cart.productid = product.productid) ;

--
David Portas
SQL Server MVP
--

Nov 23 '05 #2

P: n/a
Hi Kal,
You sure should handle the situation that the thing you are
substracting runs out of stocks, but the query should be:

UPDATE product
SET quantity = quantity - c.quantity
FROM product p
INNER JOIN Cart c
ON p.productid=c.productid
HTH, Jens Suessmeyer.

Nov 23 '05 #3

P: n/a
>
UPDATE product
SET quantity = quantity - c.quantity
FROM product p
INNER JOIN Cart c
ON p.productid=c.productid


Hi

This looked like what i wanted but it gives an error
"missing operator" FROM...

Thank

Kal
Nov 23 '05 #4

P: n/a
Jens wrote:
Hi Kal,
You sure should handle the situation that the thing you are
substracting runs out of stocks, but the query should be:

UPDATE product
SET quantity = quantity - c.quantity
FROM product p
INNER JOIN Cart c
ON p.productid=c.productid
HTH, Jens Suessmeyer.


Jens,
I think diablo will want to add "WHERE c.buyerid=x" to your query. Even
then the quantity value will not be correctly updated unless
(buyerid,productid) is unique, which it may be, although that wasn't
explicitly stated.

Diablo,
Please include DDL with future posts (CREATE TABLE, with keys and
constraints) then we don't have to makes guesses about your tables.

--
David Portas
SQL Server MVP
--

Nov 23 '05 #5

P: n/a
> > You sure should handle the situation that the thing you are
substracting runs out of stocks, but the query should be:

UPDATE product
SET quantity = quantity - c.quantity
FROM product p
INNER JOIN Cart c
ON p.productid=c.productid

Jens,
I think diablo will want to add "WHERE c.buyerid=x" to your query. Even
then the quantity value will not be correctly updated unless
(buyerid,productid) is unique, which it may be, although that wasn't
explicitly stated.


Yes - Sorry that is correct i will be adding that and buyerid is unique :-)
Diablo,
Please include DDL with future posts (CREATE TABLE, with keys and
constraints) then we don't have to makes guesses about your tables.


OK will do.

here is the query

UPDATE product_t
SET pstock = pstock - c.quantity
FROM product_t p
INNER JOIN cart_t c
ON p.productid=c.productid
where c.buyerid=7
However the issue still remains that Jens code gives a syntax error
missing operator: 'quantity - c.quantity FROM product p ...'

thanks for help

Kal

Nov 23 '05 #6

P: n/a
The following works for me. Notice the key on Cart. Jens' UPDATE only
makes sense if the productid is unique for the given buyerid -
otherwise the total deducted from Product won't match the total of the
corresponding rows in Cart.

CREATE TABLE product (productid INTEGER NOT NULL, quantity INTEGER NOT
NULL /* PRIMARY KEY not specified */) ;
CREATE TABLE cart (buyerid INTEGER NOT NULL, productid INTEGER NOT
NULL, quantity INTEGER NOT NULL, /* ?? */ PRIMARY KEY
(buyerid,productid)) ;

INSERT INTO product (productid, quantity)
VALUES (1,100) ;

INSERT INTO cart (buyerid, productid, quantity)
VALUES (1,1,90) ;

UPDATE P
SET quantity = P.quantity - C.quantity
FROM product P
INNER JOIN Cart C
ON p.productid = c.productid
WHERE C.buyerid = 1 ;

SELECT * FROM product ;

Result:

productid quantity
1 10

(1 row(s) affected)

--
David Portas
SQL Server MVP
--

Nov 23 '05 #7

P: n/a
You sure should handle the situation that the thing you are
substracting runs out of stocks, but the query should be:

UPDATE product
SET quantity = quantity - c.quantity
FROM product p
INNER JOIN Cart c
ON p.productid=c.productid
HTH, Jens Suessmeyer.


Jens,
I think diablo will want to add "WHERE c.buyerid=x" to your query. Even
then the quantity value will not be correctly updated unless
(buyerid,productid) is unique, which it may be, although that wasn't
explicitly stated.

ooops sorry buyerid is NOT unique

Cart table
CartID - Unique
BuyerID - Integer
ProductID - Integer
Quantity - Integer

Product table
ProductID - Unique
PStock - Integer
The cart table will have mutiple entries for the same buyerid. so select
'where buyerid=7' returns 3 rows from Cart table.

Sorry for the confusion

Kal
Nov 23 '05 #8

P: n/a
See the UPDATE statements I posted. There's no requirement that buyerid
be unique.

--
David Portas
SQL Server MVP
--

Nov 23 '05 #9

P: n/a
diablo (di****@noplace.com) writes:
However the issue still remains that Jens code gives a syntax error
missing operator: 'quantity - c.quantity FROM product p ...'


Then use the syntax David posted first:

UPDATE product
SET quantity = quantity -
(SELECT SUM(quantity)
FROM cart
WHERE cart.buyerid = x
AND cart.productid = product.productid)
WHERE EXISTS
(SELECT *
FROM cart
WHERE cart.buyerid = x
AND cart.productid = product.productid) ;
The syntax Jens posted works on Microsoft SQL Server, which is what we
discuss in this newsgroup. But judging from the error message you are
using some other product, unknown which. You may getter better help
if you ask in a forum for that product.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 23 '05 #10

P: n/a

Thanks the originnal one worked fine.

I am using Access 2000

Kal
Nov 23 '05 #11

P: n/a
diablo wrote:
Thanks the originnal one worked fine.

I am using Access 2000

Kal


Access is not SQL Server. Ask in the right group in future.

--
David Portas
SQL Server MVP
--

Nov 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.