Edit
Thinking about this further, I'm confident that SQL can not do exactly what you need. This answer explains why, and lists your choices:
- write application code to build a query dynamically, or
- run your existing
left join
query, and then write application code to manipulate the result into the desired format.
However, if you're comfortable combining multiple values into one column, group_concat
might get you close:
select
ssn,
name,
group_concat(distinct tel separator ', ') phones,
group_concat(distinct address separator '; ') addresses
from
(select clients.ssn,
clients.name,
cli_location.tel,
concat(cli_location.address, ', ', city, ' ', zip) address
from clients
left join cli_location
on clients.ssn=cli_location.ssn) c
group by ssn
produces:
| SSN | NAME | PHONES | ADDRESSES |
|-------|------|----------------------------------------|------------------------------------------------------------------|
| 12345 | john | 23432122, 44323455, 65765567, 98765434 | street1, city1 23443; street3, city4 76543; street2, city5 54323 |
| 54321 | alex | 86643457 | street34, city30 43234 |
http://sqlfiddle.com/#!2/ca287/25
Original
If you want a list of customers in a particular city:
select clients.ssn, clients.name
from clients
inner join cli_location
using(ssn)
where city = 'city1'
group by ssn;
http://sqlfiddle.com/#!2/ca287/3
Your schema is making this more difficult than it should be though. Consider normalizing it so that client names are in only one table, and split one-to-many relationships (like client:phone) into separate tables.