To clarify the question:
I have a table with customers which includes the first_name and last_name and the address_id. Now I have to create a list of new emails which are made of the first name and last name and the city:
firstname.lastname@city.com
I am using the sakila database btw if that is of any help
Could anybody explain to me, how I can accomplish this?
Thanks in advance!
>Solution :
The customer table has the first and last name, but not the city.
It has an address_id though, so we can join to the address table.
The address table doesn’t have the city name either, but it has a city_id.
So we join the city table to the address table.
Now with the required parts available, the new email can be constructed.
select replace(
lower(
concat(cust.first_name, '.', cust.last_name, '@', city.city, '.com')
), ' ', '_') as new_email
from customer cust
join address addr
on addr.address_id = cust.address_id
join city
on city.city_id = addr.city_id
order by new_email;
Some cities have spaces, so they were replaced.