简体   繁体   中英

Mysql compare two columns in two tables

I have 2 tables, namely Products & Purchase:

products: id, productname, quantityleft.

purchase:id, productname, quantity. 

PRODUCTS TABLE - will get all NEW products purchased. Meaning no same productname will exist in table prroducts. List of all unique Products

PURCHASE TABLE -list of all purchase products with unique Purchase_ID.

Here is my scenarios/problems:

  • If ever i want to add or insert values in PURCHASE , the table PRODUCTS will get all the values of purchase BUT only if productname in purchase is not existing in 'productname' in PRODUCTS.

  • If the productname purchase is already existed in productname products then the quantity in products will be added by the value of quantity in purchase having the same productname .

So far I have not met this situation before but maybe if I got this one it would be easy for me to code this. What I am thinking right now is to use the distinct but i know this is not the right thing to do. Can you suggest me the best and right way to achieve this one? Aside from I am really clueless, I can't googled it. Hope you could help me.

Well I will do at following way
Change the Scenario,

products: id, productname.  
purchase: id, productname, quantity, prd_id.

Item purchased; check whether name exist in products
If not exist
- assign new product id
- Insert to products and purchase table.
Else if existing item purchased (check products id)
- Nothing to products table
- Update quantity+currentQty at purchase where prd_id = products.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM