查询每个年龄的顾客消费金额数的SQL语句

来源:互联网 发布:线切割图形编程事例 编辑:程序博客网 时间:2024/04/29 09:15

有三个表,书籍表book(id,name,price)和顾客表customer(id,name,age)以及订单表orders(id,bookid,customerid)。

请写出查询每个年龄的顾客消费金额数的SQL语句。


书籍表:


顾客表:


订单表:



select `orders`.`id` AS `id`,`orders`.`bookid` AS `bookid`,`orders`.`customerid` AS `customerid`,`customer`.`name` AS `customer`,`customer`.`age` AS `age`,`book`.`name` AS `book`,`book`.`price` AS `price` from ((`orders` join `customer`) join `book`) where ((`orders`.`bookid` = `book`.`id`) and (`orders`.`customerid` = `customer`.`id`))
查询出的订单详情数据如下:



查询每个年龄的顾客消费金额数的SQL语句如下:

SELECT SUM(price),age from (SELECT price,age from orders,book,customer where orders.bookid=book.id and orders.customerid=customer.id) AS a GROUP BY age
查询出的结果如下:



0 0