日期:2014-05-17  浏览次数:20770 次

怎么把一个表里的属性名字相同的数量加起来之后得到total number,然后传到另外一个表里
需求:将表格OrderLine里相同的LPart的LQuantity相加之后,传到表格Part的Total属性里。
表格如下:
CREATE TABLE Part( /* Part description */
PNumber NUMBER(10) NOT NULL, /* Number */
PName VARCHAR(30) NOT NULL, /* Name */
PManufacturer VARCHAR(255) NOT NULL, /* Manufacturer */
PPrice NUMBER(7,2) NOT NULL, /* Price */
PRating VARCHAR(10) NULL, /* Rating */
Total NUMBER(6) NULL, /*Total number*/ 
CONSTRAINT Part_pkey PRIMARY KEY( PNumber ),
CONSTRAINT Part_fkey1 FOREIGN KEY( PRating )
);
CREATE TABLE Orders( /* Order description */
ONumber NUMBER(10) NOT NULL, /* Number */
ODate DATE NOT NULL, /* Date when issued */
OCustomer VARCHAR(255) NOT NULL, /* Customer involved */
CONSTRAINT Orders_pkey PRIMARY KEY( ONumber )
);
CREATE TABLE OrderLine( /* Ordered parts */
LOrder NUMBER(10) NOT NULL, /* Order number */
LLine NUMBER(3) NOT NULL, /* Line number */
LPart NUMBER(10) NOT NULL, /* Part number */
LQuantity NUMBER(6) NOT NULL, /* Quantity */
CONSTRAINT OrderLine_pkey PRIMARY KEY( LOrder, LLine ),
CONSTRAINT OrderLine_fkey1 FOREIGN KEY( LPart )
REFERENCES Part( PNumber ),
CONSTRAINT OrderLine_fkey2 FOREIGN KEY( LOrder )
REFERENCES Orders( ONumber )
);


------解决方案--------------------
上面的update语句写错了
SQL code
 
update part a
set total=(
select sum(LQuantity )
       from orderline
       where lpart=a.pnumber
       group by lpart
)