SELECT GROUP_CONCAT(Client) FROM (
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id1 = clients.client_id
UNION ALL
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id2 = clients.client_id
UNION ALL
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id3 = clients.client_id)
GROUP BY Client
Объяснение:
Получить клиента из одной таблицы:
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id1 = clients.client_id
Объединение получит их и из других таблиц
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id1 = clients.client_id
UNION ALL
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id2 = clients.client_id
UNION ALL
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id3 = clients.client_id)
Сейчасчтобы получить Comma-Seperated-String, либо сделайте это на стороне клиента (php), либо позвольте серверу MySQL сделать это, используя GROUP_CONCAT :
SELECT GROUP_CONCAT(Client) FROM (
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id1 = clients.client_id
UNION ALL
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id2 = clients.client_id
UNION ALL
SELECT clientName as Client
FROM deals JOIN clients
ON deals.client_id3 = clients.client_id)
GROUP BY Client