[英]Find the Maximum number in mysql
How do I find the most popular book among all customers from the table above? 如何从上表中找到所有客户中最受欢迎的书籍 ? (cid = 'customer id')?
(cid ='客户ID')?
I have 我有
select Title, sum(c.quantity) from cart c group by c.ISBN;
通过c.ISBN从cart c group中选择Title,sum(c.quantity);
which gives me the following results 这给了我以下结果
+-----------------------------------------+-----------------+ | Title | sum(c.quantity) | +-----------------------------------------+-----------------+ | Writing Skills | 5 | | Fundamentals of Database Systems | 2 | | Database Management Systems | 5 | | Data Mining, Practical Machine Learning | 4 | +-----------------------------------------+-----------------+
I know the Max() function in mysql can achieve my goal, but I do not know to implement Max() and Sum() together. 我知道mysql中的Max()函数可以实现我的目标,但我不知道一起实现Max()和Sum()。
Thanks! 谢谢!
To get most popular book/books you can use following query 要获得最受欢迎的图书/书籍,您可以使用以下查询
select c.ISBN,c.Title, sum(c.quantity) soldQuantity
from cart c
group by c.ISBN,c.Title
having soldQuantity = (
select sum(quantity)
from cart
group by ISBN,Title
order by sum(quantity) desc
limit 1
)
Note there can be more than 1 books which share same highest quantity 请注意,可以有超过1本书具有相同的最高数量
The following SQL statement should give you the book with the most quantity 以下SQL语句应该为您提供数量最多的书
SELECT
Title,
sum(c.quantity) AS total_count
FROM
cart c
GROUP BY
c.ISBN
ORDER BY
total_count DESC
LIMIT 1
Note: You really should put the books in a seperate table titled "books" with two columns, "id" and "title". 注意:你真的应该将这些书放在一个单独的表中,标题为“books”,有两列,“id”和“title”。 You can then change the "title" column in your original table to "book_id" and make it a foreign key to books.id.
然后,您可以将原始表中的“标题”列更改为“book_id”,并将其作为books.id的外键。 This will greatly improve the speed of your SQL calls.
这将大大提高SQL调用的速度。
Hope this helps! 希望这可以帮助!
Hi one approach would be using sub queries like this: 嗨,一种方法是使用这样的子查询:
SELECT TITLE, MAX (SUMMATION)
FROM (SELECT TITLE, SUM (C.QUANTITY) SUMMATION
FROM CART C
GROUP BY TITLE, C.ISBN) LIST
GROUP BY TITLE, SUMMATION
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.