code_junkie

Is it possible to take a value from a row in one table in my MySQL database and insert into another table in the same database

Nov 13th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. INSERT INTO `purchase`
  2. SET `product_id` = $whatever,
  3. `price` = (
  4. SELECT `price`
  5. FROM `product`
  6. WHERE `id` = $whatever
  7. )
  8.  
  9. INSERT INTO purchases (
  10. price
  11. ,productid
  12. ,add your other columns here
  13. )
  14. SELECT price
  15. ,id
  16. ,add your other columns here
  17. FROM products
  18. WHERE add your selection criteria here
  19.  
  20. insert into purchases(purchase_id, product_id, price)
  21. select 88, product_id, current_price from product
  22. where product_id = 'KEYB'
  23.  
  24. insert into purchases(purchase_id, product_id, price, discount)
  25. select 88, product_id, current_price, current_discount from product
  26. where product_id = 'KEYB'
Add Comment
Please, Sign In to add comment