0


I’ve been creating a database called news which contains few tables with different information in them, articles employees, pictures etc. There is two things which I’m trying to do, first is to get the three most commented articles and they need to be in order with the one with most comments first. This is the code that I’ve got which I’ve been working on. I have a table named comments which contains article ID etc.

SELECT a.ArticelID, a.preamble, a.Published, k.postcount FROM Article as a

    INNER JOIN (
 SELECT ArticelID,
    count(*) AS postcount

    FROM comments

    GROUP BY ArticelID
) as k

    on a.ArticelID = k.ArticelID


    ORDER by k.postcount DESC LIMIT 3

But it feels kind of unineffective I would only like to simply use a Join and Group By but I can’t get it through my head how it is supposed to look.

The last thing I want to do is get relevant information from an article when it comes to what pictures the article has. I have two tables, one articles and one that is pictures, what I got so far is

SELECT PictureID, Filname, photographerName, ArticelID
FROM PictureID, Articel_Picture
WHERE PictureID IN (2);

But I keep on getting error that says “Column PictureID is ambiguous” how am I supposed to handle that message?

4

1 回答 1

0

您的第一个查询很好,但您也可以将其表述为:

SELECT a.ArticelID, a.preamble, a.Published, count(*) as CommentCount
FROM Article a join
     comments c
     on a.ArticleId = c.ArticleId
ORDER by CommentCount DESC
LIMIT 3;

对于第二个查询,您需要 ajoin和别名:

SELECT p.PictureID, p.Filname, p.photographerName, ap.ArticelID
FROM PictureID p join
     Articel_Picture ap
     on p.PictureId = ap.PictureId
WHERE ap.PictureID IN (2);
于 2013-06-27T14:34:45.927 回答