MySQL:聚合函数:按连接变量分组

时间:2016-04-12 20:40:52

标签: mysql group-by aggregate-functions

我以为我把它弄下来了,但看起来我做错了什么,我无法确定是什么。

更新:我认为该错误与customer表和order_details表之间的连接有关。不确定它到底是什么,但任何见解都会很棒!

我没有收到任何错误,它只显示任何内容。是语法吗?我有什么奇怪的事吗?或者它可能是我的桌子的组织?

声明遇到问题

SELECT customers.ship_state as 'State',
        SUM(ROUND(products.product_cost * order_details.quantity_purchased, 2)) as 'Total Sales'
FROM customers
JOIN order_details on order_details.customer_id = customers.customer_id
JOIN products on products.product_id = order_details.product_id
GROUP BY customers.ship_state
ORDER BY customers.ship_state ASC;

这些是创建表声明:

CREATE TABLE customers 
(
customer_ID             int(4)              UNIQUE,
credit_card_ID          int(10)             NOT NULL,
cus_fname               varchar(35)         NOT NULL,
cus_lname               varchar(35)         NOT NULL,
company_name            varchar(35)         NOT NULL,
company_phone           varchar(15)         NOT NULL,
ship_street             varchar(50)         NOT NULL,
ship_city               varchar(25)         NOT NULL,
ship_state              varchar(2),
ship_zip                int(10)             NOT NULL,
PRIMARY KEY (customer_id),
FOREIGN KEY (credit_card_ID) REFERENCES credit_cards (credit_card_ID)
);

CREATE TABLE products 
(
product_ID              int(10)             UNIQUE,
product_name            varchar(30)         NOT NULL,           
quantity_stocked        int(5),
product_cost            dec(8,2)            NOT NULL,
PRIMARY KEY (product_ID)
);

CREATE TABLE order_details 
(
order_ID                int(10)             UNIQUE,
customer_ID             int(10)             NOT NULL,
product_ID              int(10)             NOT NULL,
quantity_purchased      int(5),
PRIMARY KEY (order_ID),
FOREIGN KEY (order_ID)      REFERENCES orders (order_ID),
FOREIGN KEY (customer_ID)   REFERENCES customers (customer_ID),
FOREIGN KEY (product_ID)    REFERENCES products (product_ID)
);

这些是插入声明:

INSERT INTO customers VALUES 
(100,1,'Sarah','Jones','Sports Authority','6197254829','1324 River Road','Cincinnati','OH',45225),
(101,2,'Desmond','Lowell','Sport Chalet','7023468547','3467 W Shaw Ave #103','Fresno','CA',93711),
(102,3,'Mark','Jefferson','24 HR Fitness','8005557000','3433 E Widget Ave','Palo Alto','CA',92711),
(103,4,'Jaime','Evans','Crash Athlete','2125554800','828 S Broadway','Tarrytown','NY',10591);

INSERT INTO products VALUES
(452,'Nike SS Pro Top',418, 22.99),
(861,'Nike LunarGlide 7',1082, 74.99),
(453,'Nike LS Pro Top',654, 64.99),
(454,'Nike SS Pro Top',720, 64.99),
(451,'Nike Pro Tank',480, 45.99),
(485,'Nike 5k Run Short',1402, 24.99),
(582,'Nike Fit Capri',750, 29.99),
(591,'Nike Be Fast Tight',1948, 39.99),
(847,'Nike Legend Tee',120, 34.99),
(277,'Nike Miller v-neck',1920, 44.99),
(135,'Adidas Soccer Pant',630, 34.99),
(456,'Adidas SS Breathe Top',754, 19.99),
(457,'Adidas Pro Cleat',626, 44.99),
(214,'Adidas Slide',902, 19.99),
(054,'Underarmor SL Top',854,54.99),
(059,'Undersarmor LS High Neck',1021,54.99),
(574,'Underarmor TR Short',746,24.99),
(421,'Underarmor TR Balance Shoe',858,84.99);

INSERT INTO order_details VALUE
(213,1,452,218),
(214,2,452,200),
(215,3,861,300),
(216,4,861,260),
(217,1,456,600),
(218,2,054,430),
(219,3,421,430),
(220,4,421,428),
(221,1,214,500),
(222,3,582,450),
(223,2,451,480), 
(224,4,861,300),
(225,3,591,800),
(226,2,277,400),
(227,1,591,800),
(228,4,054,300),
(229,2,059,500),
(230,3,059,521), 
(231,1,574,200),
(232,2,574,200),
(233,4,574,200),
(234,1,135,420),
(235,2,277,400),
(236,2,277,400);

1 个答案:

答案 0 :(得分:1)

我复制了你的表并查看了customers表!客户ID是100的。在您的订单详细信息中,您的客户ID在1中。

查询是对的。您只需要确保customers表中的客户ID与order_details表中的客户ID匹配。

我想你可以做一个解决方法:

SELECT customers.ship_state as 'State',
    SUM(ROUND(products.product_cost * order_details.quantity_purchased, 2)) as 'Total Sales'
FROM customers
JOIN order_details on order_details.customer_id = customers.credit_card_id
JOIN products on products.product_id = order_details.product_id
GROUP BY customers.ship_state
ORDER BY customers.ship_state ASC;