MySQL check if record exist then update else insert

Last Reply on Jul 14, 2012 10:31 PM By Mudassar

Posted on Jul 14, 2012 10:30 PM

hi.. i am working with vb.net, I try to find the solution for "if exist update else insert" however I could'nt find any suitable with vb.net. Can anyone help? 

2 or more tables will be affected when I click save button. 

1st. It will insert data in order_list (parent table) where order_no is the primary key. 

 

-----------------------------------------------

order_no |   From   | To

-----------------------------------------------

wo_01     |    W          |    JAT

wo_02 |    x   |    JAB

wo_03 |    Y   |    STU 

------------------------------------------------

 

 

2nd. It will insert data details in order_list_details (child table) table.  order_no with different products. eg: 

-----------------------------------------------

order_no | Product_id| Product_type

-----------------------------------------------

wo_01     |   A            |    A_A1

wo_01 |   A   |    A_A2

wo_01 |   B   |    B_B4 

wo_02 |   B   |    B_B4

wo_02 |   C   |    C_C4  

------------------------------------------------

What i did right now is 'Select Insert' to both table which then i realize that the order_no will be repeated. (I did not set order_no as primary key before realizing this matter)

Posted on Jul 14, 2012 10:31 PM

You can first fire a count query and see the count. If count > 0 update else insert