gpt4 book ai didi

mysql - 我对这个 SQL 项目感到非常迷失。汉堡店的销售订单

转载 作者:行者123 更新时间:2023-11-29 10:08:38 25 4
gpt4 key购买 nike

所以我在大学参加了 SQL 类(class),但我迷失了方向。我需要创建一个销售订单数据库,显示已售商品、客人 ID、地址、价格等...

现在,这个 Improve this question 是我到目前为止所拥有的:

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

我没有得到以下内容的预期输出:enter image description here(没有行总数)

我该从哪里开始?

附注我使用 SQL Fiddle 来完成这项作业。

更新:我的说明:提供复杂连接查询的 SQL 代码,以显示随附的“收据”中包含的所有信息以及计算属性的异常(exception)(行总计、小计、销售税和总计)。

据我了解,他们想要一次性获得所有信息?我会要求老师澄清。预期输出:

https://ibb.co/h0nP9J

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

*****点击链接 enter image description here 即可获得我认为的预期输出。**更新

我的数据很好,我能够通过内部联接查询得到它!

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;

最佳答案

编辑:花时间在 DB-Fiddle 上玩它;

数据本身很好,只是 select 语句会给你带来悲伤。

您当前的语句是 SELECT *,并在多个表上进行联接,这意味着它将从所有表中提取所有列。查看您提供的示例图像,您不需要在一个查询中呈现所有内容。

例如,订单网格不需要buyer_first_name,但select语句包含它,而Guest信息不需要item表中的PerUnit。如果我是您,我会将 SELECT * 语句分成几个较小的语句,您可以在其中明确指定要包含哪些列。

顺便说一句:“INNER JOIN”比“JOIN”更容易阅读,特别是因为它们的含义相同,并且您可能需要能够轻松地区分各种类型的 JOIN 查询。同样,buyer.buyer_middle_name 可能不应该设置为 NOT NULL。不是每个人都有中间名。 AptNumber 也是如此。

希望这能让您朝着正确的方向前进。

关于mysql - 我对这个 SQL 项目感到非常迷失。汉堡店的销售订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com