SQL内部加入组

时间:2016-10-25 13:42:47

标签: mysql sql join group-by

表 - 客户

id FirstName LastName 
23 James     Smith       
24 Tom       Raven 
25 Bob       King

表格 - 订单

id CustomerID 
30 23
31 24
32 23
33 25

表 - 产品

OrderID Product1 Product2 Product3
30      1        0        0
31      2        0        0
32      0        1        1
33      2        1        0

我想计算每位客户的产品总数,所以答案是:

CustomerID FirstName LastName Total
23         James     Smith    3
24         Tom       Raven    2
25         Bob       King     3

到目前为止,我已经得到了SQL查询:

SELECT Customers.id, Orders.id,
FROM Customers
INNER JOIN Orders ON Customers.id = Orders.CustomerID
INNER JOIN Products ON Orders.id = Products.OrderID

不知道如何计算产品数量。

非常感谢任何帮助。

6 个答案:

答案 0 :(得分:1)

select c.id as CustomerID
    ,(sum(p.Product1) + sum(p.Product2) + sum(p.Product3)) as ProductCount
from Customers c
inner join Orders o on c.id = o.CustomerID
inner join Products p on o.id = p.OrderID
group by c.id

答案 1 :(得分:1)

您可以使用

SELECT c.id as CustomerID, c.firstname, c.lastname, 
    sum(p.Product1 + p.Product2 + p.Product3) as total 
FROM Customers c
INNER JOIN Orders o
ON c.id=o.CustomerID 
INNER JOIN Products p
ON o.id=p.OrderID
group by c.id,c.firstname, c.lastname;

正如@Thorsten Kettner的评论,你应该考虑规范你的桌面设计。

答案 2 :(得分:0)

SELECT Customers.id, Customers.firstname, Customers.lastname,count(*) as total FROM Customers 
INNER JOIN Orders 
ON Customers.id=Orders.CustomerID 
INNER JOIN Products 
ON Orders.id=Products.OrderID
group by Customers.id,Customers.firstname, Customers.lastname

答案 3 :(得分:0)

添加GROUP BY条款!通常它包含的选定列不是set函数的参数。

SELECT O.CustomerID, C.FirstName, C.LastName, count(*) as Total
FROM Customers C
INNER JOIN Orders O ON C.id = O.CustomerID
INNER JOIN Products P ON O.id = P.OrderID
GROUP BY O.CustomerID, C.FirstName, C.LastName

PS。现在使用表别名,以节省一些输入。

答案 4 :(得分:0)

window.setInterval(function(){
    jQuery.get("some_local_file_url_here", function( the_page_source ) {

        jQuery("#target_div").html(the_page_source);

    });
}, 3000);

答案 5 :(得分:0)

更传统的方法可能如下:

DROP TABLE IF EXISTS customers;

CREATE TABLE customers
(customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,firstname VARCHAR(20) NOT NULL
,lastname VARCHAR(20) NOT NULL
);

INSERT INTO customers VALUES
(23,'James','Smith'),
(24,'Tom','Raven'),
(25,'Bob','King');

DROP TABLE IF EXISTS orders;

CREATE TABLE orders
(order_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,customer_id INT NOT NULL
);

INSERT INTO orders VALUES
(30 ,23),
(31 ,24),
(32 ,23),
(33 ,25);

DROP TABLE IF EXISTS order_detail;

CREATE TABLE order_detail
(order_id INT NOT NULL
,product_id INT NOT NULL
,quantity INT NOT NULL
,PRIMARY KEY(order_id,product_id)
);

INSERT INTO order_detail VALUES
(30  ,1 ,1),
(31  ,1 ,2),
(33  ,1 ,2),
(32  ,2 ,1),
(33  ,2 ,1),
(32  ,3 ,1);

SELECT c.*
     , SUM(od.quantity) total 
  FROM customers c 
  JOIN orders o 
    ON o.customer_id = c.customer_id 
  JOIN order_detail od 
    ON od.order_id = o.order_id 
 GROUP 
    BY c.customer_id;

+-------------+-----------+----------+-------+
| customer_id | firstname | lastname | total |
+-------------+-----------+----------+-------+
|          23 | James     | Smith    |     3 |
|          24 | Tom       | Raven    |     2 |
|          25 | Bob       | King     |     3 |
+-------------+-----------+----------+-------+