【发布时间】:2018-07-18 22:03:29
【问题描述】:
所以我在大学里上这门 SQL 课,但我迷路了。我需要创建一个销售订单数据库,显示已售商品、客人 ID、地址、价格等...
现在,thisenter image description here 是我目前所拥有的:
create table buyer (Guest int not NULL AUTO_INCREMENT,
buyer_first_name varchar(30) not null,
buyer_middle_name varchar(30) not null,
buyer_last_name varchar(30) not null,
address_street varchar(50) not null,
address_apt_num varchar(100) not null,
phone_home varchar(15) not null,
phone_mobile varchar(15) not null,
email varchar(50),
PRIMARY KEY (Guest));
create table item (ProductID int not NULL AUTO_INCREMENT,
PerUnit varchar(50) not null,
Product varchar(50) not null,
Details varchar(200),
PRIMARY KEY (ProductID));
create table sales_order (OrderID int not NULL AUTO_INCREMENT,
Guest int not null,
Date varchar(20),
SpecialInstructions varchar(200),
PRIMARY KEY (OrderID),
FOREIGN KEY (Guest) REFERENCES buyer(Guest)
)ENGINE=INNODB;
create table order_line (OrderID int not null,
ProductID int not null,
Amount int,
PRIMARY KEY (OrderID,ProductID),
FOREIGN KEY (OrderID) REFERENCES sales_order(OrderID),
FOREIGN KEY (ProductID) REFERENCES item(ProductID)
)ENGINE=INNODB;
insert into buyer(buyer_first_name,
buyer_middle_name ,
buyer_last_name ,
address_street ,
address_apt_num ,
phone_home ,
phone_mobile ,
email) values ('Naruto','Ninja','Uzumaki','1234 HokageDrive','1A', '1234567890', '1233126540','naruto@hotmail.com');
INSERT INTO item(Product,Details,PerUnit)
VALUES
('Big Burger Deluxe','Single Patty with Cheese','$1.99'),
('Double Big','2 Patties on a Bun','$2.99'),
('Happy Size Fries','10oz Fries w/ Seasoning','$0.99'),
('Chocolate Shake','Thick Chocolate Shake','$1.49'),
('Strawberry Shake','Thick Strawberry Shake','$1.49'),
('Cherry Pie','Deep-fried cherry Pie with Cream Cheese Icing.','$1.29');
INSERT INTO sales_order(SpecialInstructions,Guest,Date)
VALUES
('Please double bag the order.',1,'Sept. 2, 2015');
INSERT INTO order_line(OrderID,ProductID,Amount) VALUES (1,1,2);
现在我对 SQL 很不满意,希望友好地“轻推”到正确的方向。我真的很喜欢做这些东西! :)
当我跑步时:
select * from buyer a
join sales_order b on
a.Guest=b.Guest
join order_line c on
b.OrderID=c.OrderID
Join item d on
c.ProductID=d.ProductID
我没有得到这些方面的预期输出:https://ibb.co/h0nP9J(没有总行数)
我可以从哪里开始?
附:我正在使用 SQL Fiddle 来完成这项任务。
更新: 我的说明: 提供复杂连接查询的 SQL 代码,以显示随附的“收据”中包含的所有信息,并带有 计算属性的例外(行总计、小计、 销售税和总额)。
所以据我了解,他们想要一次获得所有信息?我会要求澄清形式的导师。 预期输出:
Guest ID Order ID FN MI LN Address APT# Home Cell Email Product ID Per/Unit Product Details Amount
1 1 Naruto Ninja Uzumaki 1234 Hokage Dr 1A 123 123 narutouzumaki@hotmail.com 1 1.99 Big Burger Deluxe Single patty with Cheese 2
1 1 Naruto Ninja Uzumaki 1234 Hokage Dr 1A 123 123 narutouzumaki@hotmail.com 2 2.99 Double Big 2 Patties on a Bun 2
1 1 Naruto Ninja Uzumaki 1234 Hokage Dr 1A 123 123 narutouzumaki@hotmail.com 3 0.99 Happy Size Fries 10oz Fries w/ Seasoning 4
1 1 Naruto Ninja Uzumaki 1234 Hokage Dr 1A 123 123 narutouzumaki@hotmail.com 4 1.49 Chocolate Shake Thick Chocolate Shake 1
1 1 Naruto Ninja Uzumaki 1234 Hokage Dr 1A 123 123 narutouzumaki@hotmail.com 5 1.49 Strawberry Shake Thick Strawberry Shake 3
1 1 Naruto Ninja Uzumaki 1234 Hokage Dr 1A 123 123 narutouzumaki@hotmail.com 6 1.29 Cherry Pie Deep-fried cherry Pie with Cream Cheese Icing. 2
******关注链接2,我认为这是预期的输出。** 更新
我的数据很好,我可以使用内部连接查询来处理它!
select
b.Guest,
b.buyer_first_name,
b.buyer_middle_name,
b.buyer_last_name,
b.address_street,
b.address_apt_num,
b.phone_home,
b.phone_mobile,
b.email,
s.OrderID,
s.`Date`,
o.ProductID,
i.PerUnit,
i.Product,
i.Details,
s.SpecialInstructions,
o.Amount
from buyer b
inner join sales_order s on b.Guest = s.Guest
inner join order_line o on s.OrderID = o.OrderID
inner join item i on o.ProductID = i.ProductID;
【问题讨论】:
-
尝试使用
VARCHAR(255)作为默认值,只有在你有非常令人信服的理由时才限制它。对于许多事情,例如电子邮件地址,较短的字段可能会给您的用户带来巨大的麻烦。 -
“我正在使用 SQL Fiddle 来完成这项任务。”证明它!
-
另外,您在这里混淆了两个问题:数据检索问题和数据显示问题。将它们分开,暂时忽略显示问题。修改您的问题以显示您想要的结果。
-
请将您的预期输出嵌入问题的正文中。您可以也包含一个链接,但所有必要的信息都需要在这里:1)链接可能会变得不可用,从而使这个 SO 页面无用 2)人们不想点击网络只是为了弄清楚你的问题。请
edit用代码更新您的问题,使其符合 SO 网站指南。万事如意!