seleccione 2 tablas copie un número único cuando los registros coincidan

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      batman card      NULL                    completed
2     superman card    NULL                    completed
3      spiderman card   NULL                    completed
4     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      batman card      12345678                completed
2     superman card    12345677                completed
3      spiderman card   12345676                completed
4     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

1 Respuestas

Tienes que usar UPDATEno, INSERT:

UPDATE paypal_ipn_orders
SET sort_num = (SELECT sort_num
                FROM lookup
                WHERE lookup.item = paypal_ipn_orders.item_name)

Two side notes though:

  1. Doing this will update the entire table. It would probably be best to do this in a trigger at the time of insert.

  2. I wonder the benefit of storing the sort_num twice if the sort_num will always depend on the value in your lookup table. 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 sort_numen la lookup table, but you want to keep the original values in paypal_ipn_orders.)

Respondido 30 Jul 12, 05:07

the sort id in the lookup table does change from time to time, the orders only get pulled once printed and then dropped which is why I opted to go this route. I appreciate it I'll try that. - user1542036

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.