我有一个名为deals
的表,其中存储了client_id1
,client_id2
,client_id3
。
这些ID对应clients
表,其中包含clientName
和client_id
字段。
如何在表格交易的三列中的任何一列中创建其ID为其的名称的字符串数组。
答案 0 :(得分:0)
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)
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