mysql - Trigger can't work properly when check condition -
i want store records in 2 way.
- if exists, update information
- if dosn't exist, insert new record
here outline schemas tables:
 product_purchase_item has product_purchase_item_id, product_id, quantity columns
 product_stock has  product_stock_id, product_id, product_total_quantity columns
 
i creating trigger
 delimiter //  create trigger store_check after insert on product_purchase_item  each row  begin   declare x integer;   set x =(select product_id product_stock product_id = new.product_id);   if new.product_id !=x     insert product_stock values(null,new.product_id,new.quantity);   else    update product_stock     set product_total_quantity=product_total_quantity+new.quantity    product_id=new.product_id;   end if;  end;//  delimiter;// the problem in product_stock table, insert query doesn't work when product record not exist, update query work when product record exist.
is insert statement "doesn't work"? or, problem insert statement not being executed @ all?
what happens when preceding select query not return row? value gets assigned x?
when x has value, conditional test "foo != x" return true, or return else (like false, or null)?
have tried this?
if new.product_id = x    update ... else    insert ...     end if; (i know it's bad practice answer question question; seemed apropros, since answering answer question wanted ask.)
Comments
Post a Comment