是否可以通过在类似容器的数组中返回单个表的结果来减少联接查询返回的行数
例如,对于查询:
SELECT a.col, b.col, c.col FROM
a INNER JOIN b ON a.id = b.id
INNER JOIN c ON a.id = c.id
如果已知a.id唯一匹配a中的1行,b.id匹配b中的2行,c.id匹配c中的3行,则结果为
+-------+-------+-------+
| a.col | b.col | c.col |
+-------+-------+-------+
| A0 | B1 | C1 |
| A0 | B2 | C1 |
| A0 | B1 | C2 |
| A0 | B2 | C2 |
| A0 | B1 | C3 |
| A0 | B2 | C3 |
+-------+-------+-------+
6 rows in set
所以问题是,如果它有什么可能将输出减少到类似(或类似(的东西
+-------+----------+--------------+
| A.col | b.... | c.... |
+-------+----------+--------------+
| A0 | [B1, B2] | [C1, C2, C3] |
+-------+----------+--------------+
也许不是像数组一样的结构形成字符串(对我来说像数组…(;B1、B2"C1、C2、C3";
注意,不能使用DISTINCT
,因为b.col可能包含相同的值。
如果希望表b中有多个列,例如,该怎么办
SELECT a.col, b.col, b.col2, c.col FROM
a INNER JOIN b ON a.id = b.id
INNER JOIN c ON a.id = c.id
```
how to get a result on form (or similar)
```
+-------+------------------------+--------------+
| A.col | b.... | c.... |
+-------+------------------------+--------------+
| A0 | [[B1, B11], [B2, B21]] | [C1, C2, C3] |
+-------+------------------------+--------------+
```
Would it perhaps be possible/wise to prior to `JOIN` table a and b create a temporary table btmp with the desired output (as a string or similar).
At the end of the day I wish to access a mysql data base remotely over a line that sometimes may be quite bad, so wish to reduce the amount of data sent as far as possible without loosing required information.
这是GROUP_CONNCT((的作业。事实并非如此;t创建值数组,但它确实创建了分隔字符串列表。当然,MySQL在表或结果集中都没有数组值列,所以您只能使用字符串。
我相信这会做一些接近你想要的事情。
SELECT a.col,
GROUP_CONCAT(DISTINCT b.col) b
GROUP_CONCAT(DISTINCT c.col) c
FROM a
INNER JOIN b ON a.id = b.id
INNER JOIN c ON a.id = c.id -- you had a.id = b.id but that won't work.
GROUP BY a.col