尝试这个:
SELECT SUM( p.price ) Total_quoted_value
FROM `quotes`
INNER JOIN `quote_items`
ON `quotes`.`id` = `quote_items`.`quote_id`
INNER JOIN products p
ON p.id = new_product_id
Where `quotes`.`created_by` = 5
----- 编辑 ---------- 如何计算平均价格的示例
SELECT SUM( p.price ) Total_quoted_value, AVG( p.price ) Avg_value
FROM `quotes`
INNER JOIN `quote_items`
ON `quotes`.`id` = `quote_items`.`quote_id`
INNER JOIN products p
ON p.id = new_product_id
Where `quotes`.`created_by` = 5;
sqlfiddle 演示的链接
------ 编辑 -- 一个简短的解释第二个连接在做什么 --------------
我们有三个表:
> select * from products
+ ------- + ----------------- + ---------- +
| id | product_name | price |
+ ------- + ----------------- + ---------- +
| 1 | product 1 | 20 |
| 2 | product 2 | 40 |
| 3 | product 3 | 30 |
| 4 | product 4 | 15 |
| 5 | product 5 | 5 |
+ ------- + ----------------- + ---------- +
> select * from quotes
+ ------- + --------------- +
| id | created_by |
+ ------- + --------------- +
| 1 | 5 |
| 2 | 4 |
| 3 | 5 |
| 4 | 1 |
+ ------- + --------------- +
> select * from quote_items
+ ------------- + ------------------- +
| quote_id | new_product_id |
+ ------------- + ------------------- +
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
| 2 | 2 |
| 2 | 3 |
| 2 | 5 |
| 3 | 1 |
| 3 | 3 |
| 4 | 2 |
| 4 | 4 |
| 4 | 5 |
+ ------------- + ------------------- +
第一次加入:
SELECT *
FROM `quotes`
INNER JOIN `quote_items`
ON `quotes`.`id` = `quote_items`.`quote_id` ;
+ ------- + --------------- + ------------- + ------------------- +
| id | created_by | quote_id | new_product_id |
+ ------- + --------------- + ------------- + ------------------- +
| 1 | 5 | 1 | 1 |
| 1 | 5 | 1 | 2 |
| 1 | 5 | 1 | 4 |
| 2 | 4 | 2 | 2 |
| 2 | 4 | 2 | 3 |
| 2 | 4 | 2 | 5 |
| 3 | 5 | 3 | 1 |
| 3 | 5 | 3 | 3 |
| 4 | 1 | 4 | 2 |
| 4 | 1 | 4 | 4 |
| 4 | 1 | 4 | 5 |
+ ------- + --------------- + ------------- + ------------------- +
我假设你知道一个简单的连接是如何工作的,这里不需要解释。如果没有,那么您可以在这里找到一个很好的解释:http: //www.w3schools.com/sql/sql_join_inner.asp
我们主要对第二个(组合)连接及其工作原理感兴趣。这是我们的查询:
SELECT *
FROM quotes
INNER JOIN quote_items
ON quotes.id = quote_items.quote_id
INNER JOIN products p
ON p.id = new_product_id
基本上,这个查询相当于一个简单的单连接:
SELECT *
FROM <<ResultSet_of_the_first_join>>
INNER JOIN products p
ON p.id = new_product_id
其中 < < ResultSet_of_the_first_join >> 是一个结果集(第一次连接返回的一组行 - 一种内存中的虚拟表)。
此连接(第二个)产生以下结果集:
+ ---- + ------------+ ----------+ --------------- + ----+ ------------- + ------ +
| id | created_by | quote_id | new_product_id | id | product_name | price |
+ ---- + ------------+ ----------+ --------------- + ----+ ------------- + ------ +
| 1 | 5 | 1 | 1 | 1 | product 1 | 20 |
| 1 | 5 | 1 | 2 | 2 | product 2 | 40 |
| 1 | 5 | 1 | 4 | 4 | product 4 | 15 |
| 2 | 4 | 2 | 2 | 2 | product 2 | 40 |
| 2 | 4 | 2 | 3 | 3 | product 3 | 30 |
| 2 | 4 | 2 | 5 | 5 | product 5 | 5 |
| 3 | 5 | 3 | 1 | 1 | product 1 | 20 |
| 3 | 5 | 3 | 3 | 3 | product 3 | 30 |
| 4 | 1 | 4 | 2 | 2 | product 2 | 40 |
| 4 | 1 | 4 | 4 | 4 | product 4 | 15 |
| 4 | 1 | 4 | 5 | 5 | product 5 | 5 |
+ ---- + ----------- + --------- + --------------- + ----+ ------------- + ------ +
接下来,将 WHERE 子句应用于返回的结果集以仅选择满足 WHERE 条件的行:
Where quotes.created_by = 5;
在此之后,我们得到以下信息:
+ ---- + ------------+ ----------+ --------------- + ----+ ------------- + ------ +
| id | created_by | quote_id | new_product_id | id | product_name | price |
+ ---- + ------------+ ----------+ --------------- + ----+ ------------- + ------ +
| 1 | 5 | 1 | 1 | 1 | product 1 | 20 |
| 1 | 5 | 1 | 2 | 2 | product 2 | 40 |
| 1 | 5 | 1 | 4 | 4 | product 4 | 15 |
| 3 | 5 | 3 | 1 | 1 | product 1 | 20 |
| 3 | 5 | 3 | 3 | 3 | product 3 | 30 |
+ ---- + ----------- + --------- + --------------- + ----+ ------------- + ------ +
最后查询计算列的总和和平均值price
:
SELECT SUM( p.price ) Total_quoted_value, AVG( p.price ) Avg_value
+ ----------------------- + -------------- +
| Total_quoted_value | Avg_value |
+ ----------------------- + -------------- +
| 125 | 25.0000 |
+ ----------------------- + -------------- +