MySQL Query for book cost of a class

后端 未结 6 770
清酒与你
清酒与你 2021-01-21 17:39

I just need a single MySQL line query for the following.

Lets say there are 2 simple tables: Class and Books<

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 18:08

    Did you mean something like:

    SELECT C.Name AS ClassName, SUM(B.Price) * C.Students AS BookCost
      FROM Class AS C INNER JOIN
           Books AS B ON C.ID = B.ClassId
      WHERE C.ID IN (SELECT ID
                       FROM Class
                       WHERE Students >= 31
                       ORDER BY Students ASC
                       LIMIT 1)
      GROUP BY C.ID;
    

提交回复
热议问题