I have the following table:
Name Type Value
---------------------
mike phone 123
mike address nyc
bob address nj
bob phone 333
I want to have the result like this:
name value value
-------------------
mike nyc 123
bob nj 333
How can I do it?
it is called a self-join. the trick is to use aliases.
select
address.name,
address.value as address,
phone.value as phone
from
yourtable as address left join
yourtable as phone on address.name = phone.name
where address.type = 'address' and
(phone.type is null or phone.type = 'phone')
The query assumes that each name has an address, but phone numbers are optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With