avatar
a*u
1
Question:
please, as simply as possible, design a single table to hold orders made by
customers. You may assume details (including names) about customers are
held in some other table. Using the table that you designed for orders, and
this other table, present a single query that gives the number of orders
for every customer, one line per customer.
My answer:
1. table design
Table: Orders
order_id Long int Primary key (auto increase)
customer_id Long int Foreign key
order_date date
… (other information about the order)
Table: Customers
customer_id Long int Primary key (auto increase)
customer_name string
… (other information about the customer)
2. SQL statement
SELECT Customers. customer_name, COUNT(Orders. order_id) AS Ordernum
FROM Customers
INNER JOIN Orders
ON Customers. customer_id =Orders. customer_id
ORDER BY Customers. customer_name
However, they said my answer is not correct, why?
Thank you very much!
avatar
b*e
2
ORDER BY Customers. customer_name
应该用group by customer_id, 在select语句 那里也把 customer_id 加进去。
avatar
a9
3
至少得有个给customer的订单号吧?

by
and

【在 a******u 的大作中提到】
: Question:
: please, as simply as possible, design a single table to hold orders made by
: customers. You may assume details (including names) about customers are
: held in some other table. Using the table that you designed for orders, and
: this other table, present a single query that gives the number of orders
: for every customer, one line per customer.
: My answer:
: 1. table design
: Table: Orders
: order_id Long int Primary key (auto increase)

avatar
a*u
4
So, it should be:
SELECT Customers. customer_name, COUNT(Orders. order_id) AS Ordernum
FROM Customers
WHERE Customers.customer_name = “customername”
INNER JOIN Orders
ON Customers. customer_id =Orders. customer_id
GROUP BY Customers. customer_id
Now I understand why I should use "Group By".
Thank you very much.
avatar
r*t
5
1. order by 肯定不行的,必须 group by
2. on 是不是改成 where 才对?
3. inner join 我没学过,按 sql 标准是直接 from customers, orders,问题里面有要求是啥实现没有?

by
and

【在 a******u 的大作中提到】
: Question:
: please, as simply as possible, design a single table to hold orders made by
: customers. You may assume details (including names) about customers are
: held in some other table. Using the table that you designed for orders, and
: this other table, present a single query that gives the number of orders
: for every customer, one line per customer.
: My answer:
: 1. table design
: Table: Orders
: order_id Long int Primary key (auto increase)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。