问题
I'm building a nested comments feature. I have submissions_comments which looks like:
+---------------+------------------+------+-----+---------+----------------+
| 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 | |
+---------------+------------------+------+-----+---------+----------------+'
and users which looks like:
+----------------+------------------------+------+-----+-------------------+-----------------------------+
| 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 | |
+----------------+------------------------+------+-----+-------------------+-----------------------------+
And I'm using GROUP_CONCAT and a LEFT JOIN to give me all child comments (within my submissions_comments table) with this query:
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;
which gives me:
+----+-------------------------------+----------+--------------------------------------------------------+
| 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 |
+----+-------------------------------+----------+--------------------------------------------------------+
My problem: I'm trying to join users u on submissions_comments so I can get the username, email, name, about for each parent comment and child comments.
My query:
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;
What I want:
+----+-------------------------------+----------+--------------------------------------------------------+-----------------------+
| 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 | |
+----+-------------------------------+----------+--------------------------------------------------------+-----------------------+
I keep getting these errors: ERROR 1054 (42S22): Unknown column 'parent.username' in 'field list' and ERROR 1054 (42S22): Unknown column 'child.username' in 'field list'.
回答1:
If you need usernames for both parent and child, you're going to have to go get them; they aren't in the submissions_comments table, but rather linked to that table.
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
As you can see, there are several tweaks to your query to get what you need.
回答2:
parent is an alias for submissions_comments, which has not such column username.
Only the users table has a column called username.
Change:
select ...
parent.username,
...
To:
select ...
u.username,
...
You also have a bug in your group by clause: You must add username to it get proper behaviour:
GROUP BY parent.id, u.username
来源:https://stackoverflow.com/questions/22122289/unknown-column-parent-username-in-field-list-doing-left-join-on-2-tables