I have 2 tables an item_key table and a paypal_ipn_orders table. In item_key I store item names and a *sort_id* which is an 8 digit number I use to sort the items, in the paypal_ipn_orders table I have an item name as well as a *sort_id*.
What happens
- an order comes into> paypal_ipn_orders with an ITEM name
- a query executes on the item_key table checking for a matching ITEM name in the table
- If a match exists it assigns its 8 digit value(item_key.SORT_ID) to paypal_ipn_orders.SORT_ID
I know how to cross reference the tables to find matching records when they are identical however not all if not most item titles have a slight variance ie extra spaces, a number 4 instead of 1, a the or extra character.
Query
UPDATE paypal_ipn_orders
SET sort_num = (SELECT sort_id
FROM itemkey
WHERE itemkey.item = paypal_ipn_orders.item_name)
WHERE LOWER(payment_status) = 'completed'
Result
table: paypal_ipn_orders
ITEM SORT_ID
4 Icy Manipulator ~ Ice Age NULL
4 Worldslayer - Mirrodin MtG Magic NULL
1 Karn Liberated - New Phyrexia MtG NULL
4 Blightning ~ Shards 12334234(identical title= a non-null SORT_ID)
table: item_key
ITEM SORT_ID
1 Icy Manipulator + Ice Age 12334231(doesnt exactly match)
4 Worldslayer - Mirrodin Magic 12334332(doesnt exactly match)
4 Karn Liberated - Phyrexia MtG 12334333(doesnt exactly match)
4 Blightning ~ Shards 12334234(perfect match)
Desired Result
table: paypal_ipn_orders
ITEM SORT_ID
4 Icy Manipulator ~ Ice Age 12334231(similar title = match assign value)
4 Worldslayer - Mirrodin MtG Magic 12334232(similar title = match assign value)
1 Karn Liberated - New Phyrexia MtG 12334233(similar title = match assign value)
4 Blightning ~ Shards 12334234(exact title = match assign value)
Please read about Levenshtein distance. Maybe this might help you.
Helper function: