gpt4 book ai didi

SQL查询选择下订单最多的客户

转载 作者:行者123 更新时间:2023-12-02 08:43:06 25 4
gpt4 key购买 nike

我想选择下订单最多的客户:

我有 2 个表:

CREATE TABLE customers(
customerid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME CHAR(50) NOT NULL,
address CHAR(100) NOT NULL,
city CHAR(30)NOT NULL
);


CREATE TABLE orders(
orderid INT UNSIGNED NOT NULL,
customerid INT UNSIGNED NOT NULL,
);

这就是我想要做的:

SELECT customers.customerid, customers.name,orders.orderid, COUNT(customerid) 
AS CostumerCount
FROM customers
INNER JOIN orders
ON customers.customerid=orders.orderid;

我该怎么做?

最佳答案

既然您使用的 RDBMS 不一定,那么让我假设您使用的是 MySQL,并且您正在寻找具有最高订单数:

SELECT 
customers.customerid,
customers.name,
COUNT(orders.orderid) AS Orderscount
FROM customers
INNER JOIN orders ON customers.customerid = orders.customerid
GROUP BY customers.customerid,
customers.name
ORDER BY Orderscount DESC
LIMIT 1;

关于SQL查询选择下订单最多的客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851128/

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