Forums / General Questions / Problems with query change price in 2 tables

Problems with query change price in 2 tables

Views: 1,289

Results 1 to 18 of 18
2 Oct 2012, 6:55 AM
#1
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Problems with query change price in 2 tables

I have a problem to change prices from 1 table to another table.

table 1
inventory
-itemnumber
-price

table 2
zen_products
-products_model
-products_price

in table 1 (inventory) are the right prices

it's has to compare the itemnumber with the products_model when it is the same, change the price from table zen_products with the price from tabel inventory.

I have try this for someny hours, but everytime with this query below the prices from table 2 = zero

below the query i use.

I hope somebody can help me with that.

Thanks.

UPDATE zen_products  
SET products_price =  ( SELECT inventory.Price
FROM inventory
WHERE inventory.itemNumber = zen_products.products_model)
WHERE EXISTS
  ( SELECT inventory.Price
    FROM inventory
    WHERE inventory.itemNumber = zen_products.products_model);
2 Oct 2012, 7:11 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

Why not make it a lot simpler, like this: ```
update zen_products p, inventory i
set p.products_price = i.Price
where p.products_model = i.itemNumber;

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 7:16 AM
#3
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

Thank you Dr. Byte for your fast reply

I don't know what is p and i?

2 Oct 2012, 7:17 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

Those are aliases which make it possible to avoid using tablenames repeatedly. p refers to zen_products and i refers to inventory

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 7:20 AM
#5
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

ok so i can copy exactly your query into my database
and that will be the solution?

2 Oct 2012, 7:20 AM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

I hope so :)

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 7:22 AM
#7
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

sorry, it's not working

0 rows bijgewerkt. ( query duurde 0.3136 sec )

UPDATE zen_products p,
inventory i SET p.products_price = i.Price WHERE p.products_model = i.itemNumber

2 Oct 2012, 7:28 AM
#8
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

This is a print from the 2 databasesAttachment 11241Attachment 11242

in tabel inventory are the right prices
in zen_products are the old prices

2 Oct 2012, 7:37 AM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

Your posts used upper/lower case, but your tables use only lowercase in the field names.

So rewrite the query as:

update zen_products p, inventory i set p.products_price = i.price where p.products_model = i.itemnumber;

And you must run this query in phpMyAdmin. YOU CANNOT run it in your Zen Cart Admin sqlpatch tool.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 7:42 AM
#10
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

thank you again dr. Byte, i had already see the mistake and changed that, but still not working

i do run this in phpMyAdmin

i have tryed somany solution the last 2 days, i am really frustrated

i don't know why it is not working

2 Oct 2012, 7:50 AM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

I tested it here before I posted my first solution for you, and it works fine for me.
I can in phpMyAdmin that my products_price field is correctly updated with the new prices for me.
And then in my store if I run Admin->tools->store manager->reset products price sorter, my storefront will also show the new prices correctly.

So something must be wrong with your data. Perhaps your itemnumber values don't properly match your products_model numbers. Mismatched itemnumber-vs-products_model would cause a 0 price to be entered.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 7:56 AM
#12
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

When i start de query in Phpmyadmin, there is a message
i translate this message for you.

"there is missing a value in this form'Attachment 11243

2 Oct 2012, 7:58 AM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

When did that message start? Why didn't you mention it earlier?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 8:06 AM
#14
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

I just reconstructed a new table named inventory, added an itemnumber field and price field, using the same structure as the products_model and products_price fields in my zen_products table. Then I filled the inventory table with some sample entries, using itemnumbers that match products_model values.
I used phpMyAdmin to run the same query I posted above, and it worked without any error.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Oct 2012, 8:07 AM
#15
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

because the second time i started the query in the startscreen (localhost » jvw_testdb) , i don't know how to tell you.

the first try with the query i was in the table inventory ( localhost » jvw_testdb » inventory) and after that i push the SQL button,
and start the query, there was no message and no changes in the prices

the last try with the message i was here in ( localhost » jvw_testdb) and started the same query, with result that message.

2 Oct 2012, 9:51 AM
#16
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

i have found the problem why it is not working,

the field products_model in zen_products align to the left
the field itemnumber in inventory is floating?

How i don't know?

But now, how can i change the field itemnumber in inventory, also the the left?

thank you!Attachment 11245Attachment 11246

2 Oct 2012, 2:15 PM
#17
petje007 avatar

petje007

New Zenner

Join Date:
Dec 2008
Location:
Amsterdam, The Netherlands
Posts:
21
Plugin Contributions:
0

Re: Problems with query change price in 2 tables

Hello Dr. Byte, it's just like you said, it's working.

I don't no what's happened that at beginning of the field 'itemnumber' there were al lot of spaces.
I have removed the spaces and use this query from you:

update zen_products p, inventory i set p.products_price = i.price where p.products_model = i.itemnumber;

in 0,5 seconds more then 33000 prices were changed!. Is that not GREAT news:smile:

Thank you to point me at "So something must be wrong with your data. Perhaps your itemnumber values don't properly match your products_model numbers. Mismatched itemnumber-vs-products_model would cause a 0 price to be entered"

End yes, the data was mismatched.

Again Dr. Byte, thank you so much for your time and help!!

2 Oct 2012, 3:15 PM
#18
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Problems with query change price in 2 tables

:) Glad you got it sorted out.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.