0

I have two tables: COMMENTS and USERS. I save user's name on COMMENTS table, but user's avatar details should be taken from USERS table.

SELECT c.id, c.tarih, c.yorum, c.yorumcu, 
(SELECT avatarID FROM USERS WHERE Username=c.yorumcu) AS avatarID 
FROM COMMENTS c WHERE c.id = ?

The question mark at the end gets replaced with NEWS_ID. (e.g news.php?id=5)

I managed to code this, but it only gets the "last comment" details from database, not all of them. (Let's say 5 people commented on page, it only gets the last (5th) comment, ignoring the rest.)

Can anybody help me?

Ps. Yorumcu means "Commenter". COMMENTS.Commenter and USERS.Username are the same.

3 Answers 3

2

Should your WHERE clause not be "WHERE c.news_id = ?", rather than "WHERE c.id = ?" ? I am assuming that c.id is unique to each comment.

Sign up to request clarification or add additional context in comments.

1 Comment

I feel stupid now. Thanks alot. I was checking comment ID instead of news_id. Were about to go crazy...
0

try this:

select c.id, c.tarih, c.yorum, c.yorumcu, avatarID 
FROM COMMENTS,USERS 
where USERS.Username=c.yorumcu  and c.id = ?

1 Comment

Thanks for your effort. The query above worked like a charm after I change c.id to c.news_id.
0

I assume that COMMENTS.id is the unique identifier for the table COMMENTS?

If that is the case, it is logic, that you only get returned 1 comment.

Do you have a identifier for NEWSID in the COMMENTS table? Then you should use this one to retrieve multiple comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.