SQL query involving group by and joins

后端 未结 3 616
耶瑟儿~
耶瑟儿~ 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:19

    Try this (modified for projects with no offers):

    SELECT
      Project.addDate,
      Project.idOwner,
      Account.Username,
      Project.idProject,
      Project.Price,
      ISNULL(q.offercount, 0) AS offercount
    FROM
      (
        SELECT
          o.idProject,
          COUNT(o.idProject) as offercount
        FROM Offer o
        GROUP BY o.idProject
      ) AS q
      RIGHT JOIN Project ON Project.idProject = q.idProject
      INNER JOIN Account ON Account.idAccount = Project.idOwner
    ORDER BY addDate DESC
    

提交回复
热议问题