Jump to content

Problem with Inserting data in tables


anser316

Recommended Posts

Hi, I have a problem with inserting data into my 'total_stock' table. I want to calculate 'total_stock' by adding all 'stock'. I have been trying to insert data in 'stocks' table, within inserting data in 'total_stock' as i cant add into stock table first as i need the foreign which i havent created.

 

 

create table total_stock(
t_id                 smallint(2) unsigned not null,
total_stock    float not null,
constraint total_pk primary key (t_id));

create table stocks
s_id      smallint(2) unsigned not null,
stock    int not null,
t_id      smallint(2) unsigned not null,
constraint stocks_pk primary key (s_id)
constraint stocks_tid_fk foreign key (t_id)
REFERENCES total_stock(t_id));

Link to comment
https://forums.phpfreaks.com/topic/98617-problem-with-inserting-data-in-tables/
Share on other sites

insert into table_stock (t_id) values (1);

insert into stocks (s_id,stock,t_id) values (1,10,1);
insert into stocks (s_id,stock,t_id) values (2,20,1);

UPDATE table_stock SET total_stock=SUM(stock)
WHERE t_id=1

 

I dont know how to sum the stock and store it properly

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.