1

I'm having trouble getting my head around how to handle something I've thought of in SQL. I have bookmarks, and comments that the users posted about them. I'm using a single table for all comments. So we have a one-to-many relationship between the bookmarks and the comments.

There is another table, acting as the middle-man, linking each bookmark to all its comments.

There are two types of comments. Suggested titles for the bookmark, and general comments. Suggested titles have both a title and a description, while general comments have only a description. There's also a rating system for the suggested titles, so that the home page can pick the top-rated title for each bookmark to display.

So, main things to make clear. There's the Bookmarks table with BID and URL, and also the Comments table with CID, Title, Comment, and Rating. The BooksNComms is the connecting table between them.

    SELECT comments.title, comments.comment
      FROM comments
INNER JOIN booksncomms ON comments.cid=booksncomms.cid
     WHERE booksncomms.bid=1
       AND comments.title is not null
  ORDER BY comments.rating
     LIMIT 0, 1;

The above works in getting the best Title and Description (Comment) for a certain BID. What I want to do is make the above work for, say, the 10 newest bookmarks.

    SELECT bookmarks.url, comments.title, comments.`comment`, comments.rating
      FROM bookmarks
INNER JOIN booksncomms 
        ON bookmarks.bid=booksncomms.bid
INNER JOIN comments 
        ON comments.cid=booksncomms.cid
      JOIN (
            SELECT bookmarks.bid 
              FROM bookmarks 
          ORDER BY bookmarks.datecreated DESC 
             LIMIT 1
        )
        AS a 
        ON a.bid=bookmarks.bid
     WHERE comments.title IS NOT NULL
  ORDER BY bookmarks.url;

The above gives me all titles for the 10 newest bookmarks.

Is there a way I can select only the highest rated title for each of the 10 newest bookmarks?

3
  • Unreadable SQL. It's a convention that SQL keywords should be in capital letters to distinguish them. :(
    – Shef
    Jul 22, 2011 at 19:42
  • Edited my second example as per your instructions. Sorry for that. Jul 22, 2011 at 19:50
  • Updated my answer, try that and let me know if it works or not.
    – Shef
    Jul 22, 2011 at 20:04

2 Answers 2

1

(OP's own solution, split off from the question)

@LefterisAslanoglou says:

I realized I knew the answer just a few minutes after I posted the question here. It's been bugging me for hours, but it was a simple matter of getting a table that has the best rated title for each bookmark and then joining that to the one with all the titles for the latest bookmarks.

    SELECT bookmarks.url, comments.title, comments.comment, comments.rating
      FROM bookmarks
INNER JOIN booksncomms ON bookmarks.bid=booksncomms.bid
INNER JOIN comments ON comments.cid=booksncomms.cid
      JOIN (
           SELECT bookmarks.bid 
             FROM bookmarks 
         ORDER BY bookmarks.datecreated DESC 
            LIMIT 10
      ) AS a ON a.bid=bookmarks.bid
      JOIN (
          SELECT comments.cid, MAX(comments.rating) 
              AS maxrating
            FROM comments
      INNER JOIN booksncomms ON comments.cid=booksncomms.cid
        GROUP BY booksncomms.bid
      ) AS b ON b.cid=comments.cid
     WHERE comments.title IS NOT NULL
  ORDER BY bookmarks.datecreated DESC;
1
  • Thanks for posting my answer separately. It's been quite some time since I worked on this project... Whenever I look back to it, I always laugh at my choice for a separate table for a one-to-many relationship. Oh well! May 10, 2013 at 22:35
0

Try

    SELECT bookmarks.url, comments.title, comments.`comment`, MAX(comments.rating) rating
      FROM bookmarks
INNER JOIN booksncomms 
        ON bookmarks.bid = booksncomms.bid
INNER JOIN comments 
        ON comments.cid = booksncomms.cid
     WHERE comments.title IS NOT NULL
  ORDER BY bookmarks.datecreated DESC, bookmarks.url
     LIMIT 10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.