我正在构建嵌套评论功能。我有submissions_comments
看起来像:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| submission_id | int(11) | NO | MUL | NULL | |
| comment | text | NO | | NULL | |
| parent_id | int(10) unsigned | YES | MUL | NULL | |
| created | datetime | NO | MUL | NULL | |
| created_ip | int(11) | NO | | NULL | |
| helpful_count | int(11) | NO | MUL | NULL | |
| deleted | tinyint(4) | NO | MUL | 0 | |
+---------------+------------------+------+-----+---------+----------------+'
和看起来像的用户:
+----------------+------------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------------+------+-----+-------------------+-----------------------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| email | varchar(128) | NO | MUL | NULL | |
| username | varchar(23) | NO | | NULL | |
| name | varchar(32) | NO | | NULL | |
| about | varchar(255) | NO | | NULL | |
+----------------+------------------------+------+-----+-------------------+-----------------------------+
我正在使用GROUP_CONCAT
和LEFT JOIN
通过此查询向我提供所有儿童评论(在我的submissions_comments
表格内):
SELECT parent.id, MAX(parent.comment) as pcomm,
GROUP_CONCAT(child.id ORDER BY child.id) as children,
GROUP_CONCAT(child.comment ORDER BY child.id) as childrenComments
FROM submissions_comments AS parent
LEFT JOIN submissions_comments AS child
ON child.parent_id = parent.id
WHERE parent.parent_id IS NULL
GROUP BY parent.id
ORDER BY parent.id;
给了我:
+----+-------------------------------+----------+--------------------------------------------------------+
| id | pcomm | siblings | siblingComments |
+----+-------------------------------+----------+--------------------------------------------------------+
| 1 | This is a parent | 2,4 | This is a child comment,This is a second child comment |
| 3 | I don't have any children | NULL | NULL |
| 5 | Testing one two three | NULL | NULL |
| 6 | adsdfsasdf | NULL | NULL |
| 7 | asdfadsfdsaf | NULL | NULL |
| 8 | asdfasdsadfsadf | NULL | NULL |
| 9 | asdfsdafsdafdaafds | NULL | NULL |
+----+-------------------------------+----------+--------------------------------------------------------+
我的问题:我正在users u
加入submissions_comments
,因此我可以获得username
,email
,{{1每个父评论和子评论都有,name
。
我的查询:
about
我想要的是什么:
SELECT parent.id, MAX(parent.comment) as pcomm,
parent.username,
GROUP_CONCAT(child.id ORDER BY child.id) as children,
GROUP_CONCAT(child.comment ORDER BY child.id) as childrenComments,
GROUP_CONCAT(child.username ORDER BY child.id) as childrenUsernames
FROM submissions_comments AS parent
LEFT JOIN submissions_comments AS child
ON child.parent_id = parent.id
LEFT JOIN users u on parent.user_id = u.id
GROUP BY parent.id
ORDER BY parent.id;
我一直收到这些错误:ERROR 1054(42S22):'字段列表'和ERROR 1054(42S22)中的未知列'parent.username':'字段列表'中的未知列'child.username'。
答案 0 :(得分:1)
parent
是submissions_comments
的别名,其中没有此列username
。
只有users
表有一个名为username
的列。
变化:
select ...
parent.username,
...
要:
select ...
u.username,
...
您的group by
子句中也有一个错误:您必须添加username
才能获得正确的行为:
GROUP BY parent.id, u.username
答案 1 :(得分:1)
如果您需要parent
和child
的用户名,那么您将不得不去获取它们;它们不在submissions_comments表中,而是链接到该表。
SELECT parent.id, MAX(parent.comment) as pcomm,
pu.username,
/* display child comments in order of child USER id */
GROUP_CONCAT(cu.id ORDER BY cu.id) as children,
GROUP_CONCAT(child.comment ORDER BY cu.id) as childrenComments,
GROUP_CONCAT(cu.username ORDER BY cu.id) as childrenUsernames
/* handle parent submissions, with user identity */
FROM submissions_comments AS parent
LEFT JOIN users AS pu ON parent.user_id = u.id /* get parents' user info */
/* handle child submissions, with user identity */
LEFT JOIN submissions_comments AS child ON child.parent_id = parent.id
LEFT JOIN users AS cu ON child.user_id = cu.id /* get children's user info */
GROUP BY parent.id, pu.username /* avoid stupid nonstandard MySQL GROUP BY */
ORDER BY parent.id, pu.username
正如您所看到的,您可以通过几种调整来获得所需内容。