gpt4 book ai didi

php - php mysql 每天找出三个最畅销的产品

转载 作者:行者123 更新时间:2023-11-29 06:51:27 25 4
gpt4 key购买 nike

我想找到每天销量最高的三种产品,并且将它们与销售数量一起显示。

但是,如果有多个产品共享相同的销售数量 我只想告诉有多少产品得到了这个排名。

我有两张 table

Products:
+-----+---------+
| Pid | Product |
+-----+---------+
| 1 | Moon |
| 2 | Sun |
| 3 | Venus |
| 4 | Mars |
+-----+---------+

SalesRows:
+-----+---------+------------+
| Pid | No_sold | Sales_date |
+-----+---------+------------+
| 1 | 1 | 2013-01-01 |
| 2 | 5 | 2013-01-01 |
| 3 | 2 | 2013-01-01 |
| 2 | 2 | 2013-01-01 |
+-----+---------+------------+

应该给:

+------+--------------+-------+
| Rank | Product | Sales |
+------+--------------+-------+
| 1 | Sun | 7 |
| 2 | Venus | 2 |
| 3 | Moon | 1 |
+------+--------------+-------+

然而这个销售数据:

SalesRows:
+-----+---------+------------+
| Pid | No_sold | Sales_date |
+-----+---------+------------+
| 1 | 1 | 2013-01-01 |
| 2 | 5 | 2013-01-01 |
| 3 | 2 | 2013-01-01 |
| 2 | 2 | 2013-01-01 |
| 4 | 1 | 2013-01-01 |
+-----+---------+------------+

应该给:

+------+--------------+-------+
| Rank | Product | Sales |
+------+--------------+-------+
| 1 | Sun | 7 |
| 2 | Venus | 2 |
| 3 | *2 products* | 1 |
+------+--------------+-------+

对如何解决最后一部分有什么建议吗?

最佳答案

此查询可能对您有所帮助。

SELECT @rownum := @rownum + 1 rownum, 
t.*
FROM (SELECT @rownum:=0) r,
(select case when indicator = 1 then Product
else concat( indicator, ' Products') end as Product, sales from (Select *, count(sales) as indicator from (SELECT Product,SUM(No_sold) AS sales FROM SalesRows
JOIN Products ON Products.Pid = SalesRows.Pid
WHERE Sales_date = curdate()
GROUP BY SalesRows.Pid ) a group by sales Order by sales desc) a) t

关于php - php mysql 每天找出三个最畅销的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15152587/

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