here is two tables:
这是两个表:
a:
A:
+-----+------------------------+
| id | conten |
+-----+------------------------+
| 1 | q. |
| 2 | q. |
| 3 | s. |
| 4 | g |
| 1 | a |
| 2 | a |
+-----+------------------------+
b:
b:
+-----+------------------------+
| id | type |
+-----+------------------------+
| 1 | I. |
| 2 | II. |
| 3 | III. |
| 4 | IV |
| 5 | V |
| 6 | VI |
+-----+------------------------+
Is there a way to select from a and b so that for one id 2
, there will be one additional field that groups all content from that id? the select result should be something like this:
有没有办法从a和b中进行选择,以便对于一个id 2,会有一个附加字段对该ID中的所有内容进行分组?选择结果应该是这样的:
+-----+------------------------+-----------+
| id | type | contents |
+-----+------------------------+-----------+
| 2 |I. | q,a |
+-----+------------------------+-----------+
Edited btw, if there is a way to do it by sqlahcmey, that would be sweet.
编辑顺便说一句,如果有一种方法可以通过sqlahcmey来做,那将是甜蜜的。
1 个解决方案
#1
1
SELECT b.id, b.type, IFNULL(GROUP_CONCAT(a.conten), '') AS contents
FROM b
LEFT JOIN a ON a.id = b.id
GROUP BY b.id
See How do I write a group_concat function in sqlalchemy? for how to translate GROUP_CONCAT
to SQLAlchemy.
请参阅如何在sqlalchemy中编写group_concat函数?如何将GROUP_CONCAT转换为SQLAlchemy。
#1
1
SELECT b.id, b.type, IFNULL(GROUP_CONCAT(a.conten), '') AS contents
FROM b
LEFT JOIN a ON a.id = b.id
GROUP BY b.id
See How do I write a group_concat function in sqlalchemy? for how to translate GROUP_CONCAT
to SQLAlchemy.
请参阅如何在sqlalchemy中编写group_concat函数?如何将GROUP_CONCAT转换为SQLAlchemy。