【发布时间】:2014-03-02 00:24:07
【问题描述】:
我正在构建一个嵌套的 cmets 功能。我有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 为我提供所有子 cmets(在我的 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 |
+----+-------------------------------+----------+--------------------------------------------------------+
我的问题:我正在尝试在submissions_comments 上加入users u,这样我就可以获得username、email、name、about 对于每个家长评论和子 cmets。
我的查询:
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;
我想要什么:
+----+-------------------------------+----------+--------------------------------------------------------+-----------------------+
| id | pcomm | children | childrenComments |childrenUsernames |
+----+-------------------------------+----------+--------------------------------------------------------+-----------------------+
| 1 | This is a parent | 2,4 | This is a child comment,This is a second child comment |blahbster, user123 |
| 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 | |
+----+-------------------------------+----------+--------------------------------------------------------+-----------------------+
我不断收到以下错误:错误 1054 (42S22):“字段列表”中的未知列“parent.username”和错误 1054 (42S22):“字段列表”中的未知列“child.username”。
【问题讨论】: