Pregunta

I have three tables: Comments, Users and CommentsHelpfulness.

Users can submit several comments, which are stored in the Comments table.

CommentsHelpfulness has three columns: UserID, CommentID and a "Helpful" Boolean. Every user can indicate for every comment if they find it useful, which will create an entry in the CommentsHelpfulness table.

I want a query that gives me all Comment IDs, with the name of the user that submitted it and shows whether the currently logged in user found it helpful, did not find it helpful or did not say anything about it. So the ID of a comment the current user did not express his opinion about should still be output, just without the helpful Boolean.

To me that sounds like it should be done like this using a left join:

SELECT Comments.ID, Users.Nom, CommentsHelpfulness.Helpful FROM (Comments INNER JOIN Users ON Comments.UserID = Users.ID) LEFT JOIN CommentsHelpfulness ON (CommentsHelpfulness.CommentID = Comments.ID AND (CommentsHelpfulness.UserID = ?))

Unfortunately this does not output Comment IDs without an entry in the CommentsHelpfulness table. Why does this not work? Is it because of Access?

¿Fue útil?

Solución

I think the issue is the inner join, not the left join.

Try removing that table:

SELECT c.ID, ch.Helpful
FROM Comments as c LEFT JOIN
     CommentsHelpfulness as ch
     ON ch.CommentID = c.ID AND
        ch.UserID = ?

Otros consejos

select c.id, c.Nom, d.Helpful from (
select a.ID, b.Nom from Comments a left join Users b
on a.UserID = b.ID) c left join CommentsHelpfulness d
on d.CommentID = c.ID;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top