We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,780 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

MYSQL update query

I need to update values form one table to another and create values if they are not there
How can I do this for all values or form values from one ID to another

This is the query but I need to run it for every ID and that is a lot of job, so is there any simplier way?

So id_product is always the same

`

INSERT INTO ps_product_supplier 
(id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency)  
VALUES(NULL, 6216, 0, (SELECT id_supplier FROM ps_product WHERE id_product = 6216), (SELECT supplier_reference FROM ps_product WHERE id_product = 6216), 
(SELECT wholesale_price FROM ps_product WHERE id_product = 6216), 3);

`

2
Contributors
11
Replies
1 Week
Discussion Span
4 Months Ago
Last Updated
12
Views
Question
Answered
mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Use a select from the ps_product table to insert into ps_product_supplier. Something like this:

INSERT INTO ps_product_supplier 
(id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency)  

SELECT
    NULL, id_product, 0, id_supplier, supplier_reference,  wholesale_price , 3
FROM 
    ps_product
AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

Wihout any restrictions?
With this I will owerwrite id_product?

mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What this script does is:

Select the specified columns from ps_products, and insert each returned row into ps_product_suplier.

The table ps_product will not be changed.

AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

But it does not inserts anything in table, any ideas?

mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Or can I write PHP to do this?

mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Does it throw any errors?
First test only the select part and see if it returns any rows.

Yes, you could do it with PHP. You'd have to make a select on the rows you want to insert, then loop thru them and insert each record.

AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

It returns 0 rows changed?

mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This select return 0 rows?

SELECT
    NULL, id_product, 0, id_supplier, supplier_reference,  wholesale_price , 3
FROM 
    ps_product

If so, it means that there's no rows in your table.

AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

That does not work
this is in ps_product_supplier:

id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency

NULL is there for id_product_supplier

mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

Yes, I will try
Thanks

mehnihma
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Months Ago by AleMonteiro

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0969 seconds using 2.7MB