I have 2 tables 1. paypal_ipn_orders table 2 is lookup. The lookup table I have a list of all of my items with the column name item I also have another column labeled sort_id which is an 8 digit number used internally for all of my items. Table 1 paypal_ipn_orders receives orders and has several columns but only a few are important to the question. id, payer_email, item_name, sort_num and payment_status. What I need to happen is when an order comes into table paypal_ipn_orders I need to cross reference the lookup table and assign the 8 digit number into the column labeled sort_num when the item names match.
table: paypal_ipn_orders id payer_email item_name sort_num payment_status 1 firstname.lastname@example.org batman card NULL completed 2 email@example.com superman card NULL completed 3 firstname.lastname@example.org spiderman card NULL completed 4 email@example.com captain america NULL completed
here is the sample look up table
table: lookup item sort_id batman card 12345678 superman card 12345677 spiderman card 12345676 captain america 12345675
what I want the output to look like after I run the query.
table: paypal_ipn_orders id payer_email item_name sort_num payment_status 1 firstname.lastname@example.org batman card 12345678 completed 2 email@example.com superman card 12345677 completed 3 firstname.lastname@example.org spiderman card 12345676 completed 4 email@example.com captain america 12345675 completed
I know this isnt exactly the right syntax but you'll get the idea
SELECT lookup.sort_id INSERT INTO paypal_ipn_orders.sort_num WHERE lookup.item = paypal_ipn_orders.item_name
preguntado el 30 de julio de 12 a las 04:07
Tienes que usar
UPDATE paypal_ipn_orders SET sort_num = (SELECT sort_num FROM lookup WHERE lookup.item = paypal_ipn_orders.item_name)
Two side notes though:
Doing this will update the entire table. It would probably be best to do this in a trigger at the time of insert.
I wonder the benefit of storing the
sort_numtwice if the
sort_numwill always depend on the value in your
lookuptable. If you are trying to save the value at the time of insert or be able to manually change it though, it makes sense. (That is, if you change the
lookuptable, but you want to keep the original values in