avatar
Question for SQL statment# Database - 数据库
x*g
1
table purchase_order(po_id,user)
table purchase_order_item(po_id,poi_id,item_id,number)
table stock(item_id,number_in_stock)
how to update the stock table to deduct the number_in_stock for a
purchase_order? (with one statment)
thanks
and which book or document is the best to learn SQL about these matters?
avatar
a*o
2
if give me the po_id, then
update stock set number_in_stock = s.number_in_stock -
poi.number
from purchase_order_item poi inner join stock s
on s.item_id = poi.item_id
where poi.po_id = xxxxxx
this is ANSI SQL. If you use Oracle, replace inner join with
implicit join.
Any SQL book can help in your case.

【在 x****g 的大作中提到】
: table purchase_order(po_id,user)
: table purchase_order_item(po_id,poi_id,item_id,number)
: table stock(item_id,number_in_stock)
: how to update the stock table to deduct the number_in_stock for a
: purchase_order? (with one statment)
: thanks
: and which book or document is the best to learn SQL about these matters?

avatar
s*g
3
What if we want to deduct all the number_in_stock of all the items in the
PO? Multiple items, rather than just one.
Thanks!

【在 a****o 的大作中提到】
: if give me the po_id, then
: update stock set number_in_stock = s.number_in_stock -
: poi.number
: from purchase_order_item poi inner join stock s
: on s.item_id = poi.item_id
: where poi.po_id = xxxxxx
: this is ANSI SQL. If you use Oracle, replace inner join with
: implicit join.
: Any SQL book can help in your case.

avatar
p*s
4

I guess UPDATE doesn't work unless you can do this
UPDATE stock
SET number_in_stock =
number_in_stock - (SELECT count(po_id)
FROM purchase_order_item I,
purchase_order O
WHERE I.po_id = O.po_id
GROUP BY I.item_id)
However, I don't think nowadays SQL implementations can achieve this.
If we start f

【在 x****g 的大作中提到】
: table purchase_order(po_id,user)
: table purchase_order_item(po_id,poi_id,item_id,number)
: table stock(item_id,number_in_stock)
: how to update the stock table to deduct the number_in_stock for a
: purchase_order? (with one statment)
: thanks
: and which book or document is the best to learn SQL about these matters?

avatar
s*g
5
What about the following query?
update stock
set number_in_stock = s.number_in_stock-poi.number
from purchase_order_item poi, stock s
where poi.item_id=s.item_id

【在 a****o 的大作中提到】
: if give me the po_id, then
: update stock set number_in_stock = s.number_in_stock -
: poi.number
: from purchase_order_item poi inner join stock s
: on s.item_id = poi.item_id
: where poi.po_id = xxxxxx
: this is ANSI SQL. If you use Oracle, replace inner join with
: implicit join.
: Any SQL book can help in your case.

avatar
p*s
6

^^^^^^^^^^
It depends on the meaning of this "poi.number", if this is
a derived attribute recording count of purchase_order for the item,
then the UPDATE statement works.

【在 s*****g 的大作中提到】
: What about the following query?
: update stock
: set number_in_stock = s.number_in_stock-poi.number
: from purchase_order_item poi, stock s
: where poi.item_id=s.item_id

avatar
a*o
7
That's it.

【在 s*****g 的大作中提到】
: What about the following query?
: update stock
: set number_in_stock = s.number_in_stock-poi.number
: from purchase_order_item poi, stock s
: where poi.item_id=s.item_id

avatar
a*c
8
You should consider the required query is used for one
order, not all the orders. Add another table purchase_order
into your update statement.

where po.po_id = poi.po_id
and poi.item_id=s.item_id

【在 a****o 的大作中提到】
: That's it.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。