SQL query involving group by and joins

后端 未结 3 624
耶瑟儿~
耶瑟儿~ 2021-01-19 10:54

I couldn\'t be more specific in the title part but I want to do something a little bit complex for me. I thought I did it but it turned out that it is buggy.

I have

3条回答
  •  忘掉有多难
    2021-01-19 11:18

    Your question is a bit vague, but here are some pointers:

    • To list the projects "with most offers made", ORDER BY offercount.
    • You're essentially querying for projects, so you should GROUP BY Project.idProject first before the other fields.
    • You're querying for the number of offers made on each project, yet you ask about offer details. It doesn't really make sense (syntax-wise) to ask for the two pieces of information together. If you want to get the total number of offers, repeated in every record of the result, along with offer information, you'll have to use an inner query for that.

    An inner query can be made either in the FROM clause, as suggested by other answers, or directly in the SELECT clause, like so:

    SELECT Project.idProject,
           (SELECT COUNT(Offer.idOffer)
              FROM Offer
             WHERE Offer.idProject = Project.idProject
            ) AS OfferCount
      FROM Project
    

提交回复
热议问题