我有一张桌子" Car"我有两列Cname和Ccolor。我希望这个表或一个新表包含一个具有共同颜色的汽车列表,并动态绑定。
我有什么:+---------+--------+
| Cname | Ccolor |
+---------+--------+
| Ferrari | Red |
| Maruti | Red |
| Phantom | Red |
| Duster | Black |
| Beetle | Black |
| Hummer | White |
| Skoda | White |
| Pajero | White |
+---------+--------+
我想要实现的目标:
+---------+--------+-----------------+
| Cname | Ccolor | CommonColorCar |
+---------+--------+-----------------+
| Ferrari | Red | Maruti Phantom |
| Maruti | Red | Ferrari Pahntom |
| Phantom | Red | Ferrari Maruti |
| Duster | Black | Beetle |
| Beetle | Black | Duster |
| Hummer | White | Skoda Pajero |
| Skoda | White | Hummer Pajero |
| Pajero | White | Hummer Skoda |
+---------+--------+-----------------+
答案 0 :(得分:0)
我想不出你为什么要连接这个结果,但无论如何......
SELECT x.*
, GROUP_CONCAT(y.name)
FROM my_table x
LEFT
JOIN my_table y
ON y.color = x.color
AND y.name <> x.name
GROUP
BY x.name
, x.color;