I have a table/sql issue I don’t know how to solve.
I need to update/create a table of user ids with order ids.
Therefore I have to get a new user_id, by searching for the email in an old list.
With the email adress I need to look up the new user id.
So the logic is like:
order_id -> look at the old user_id -> look at the email -> look at the new user_id
I tried to create an example:
---------------------
TABLE: USERS_OLD (a list of user ids and an email adress)
id email
1 [email protected]
2 [email protected]
3 [email protected]
4 [email protected]
---------------------
---------------------
TABLE: USER_ORDERS_OLD (the connection of an order id with a user id)
user_id order_id
1 DLEFGM
2 OPDFGT
3 UZDFGP
4 POIDSX
---------------------
---------------------
TABLE: USERS_NEW (a new list of users id with the same emails from table USERS_OLD)
id email
5 [email protected]
9 [email protected]
10 [email protected]
17 [email protected]
---------------------
What I want to create:
---------------------
TABLE: USER_ORDERS_NEW
user_id order_id
5 DLEFGM
9 OPDFGT
10 UZDFGP
17 POIDSX
---------------------
I have no idea how to do that action. I don’t even know what to search for.
What I managed to do is a LEFT JOIN sql statement to compare the user ids and create a list of matching user_ids.
But I have no idea how to look up over even more tables…
Hopefully someone can help me.
If it’s easier I could also try to do it in spreadsheets.
Thanks in advance!
2
Answers
With your example you should be able to accomplish this with this query
You may want to opt for a LEFT JOIN on the old order table if you intend to migrate the ids regardless of whether they have had an order or not although I assume that’s not the intention to populate the new order table with new users ids that haven’t made an order.
Assuming you just want to return the query then use;
However, if you want to write the result into a new table, then you need to create the table first.
See Demo