mysql - MySQL-如何使用 Group By 获取值
问题描述
+-----------------+--------+-------+
| Date(yyyy-mm-dd)| name | price |
+-----------------+--------+-------+
| 2021-03-01 | APPLE | 15 |
| 2021-03-01 | Orange | 10 |
| 2021-03-01 | Banana | 5 |
| 2021-03-02 | Apple | 16 |
| 2021-03-02 | Orange | 9 |
| 2021-03-02 | Banana | 4 |
| 2021-03-03 | Apple | 17 |
| 2021-03-03 | Orange | 11 |
| 2021-03-03 | Banana | 5 |
+-----------------+--------+-------+
**Fruit Value**
我正在尝试获取价格每天都在上涨的水果的名称。所以输出应该是“苹果”,因为苹果在 3 月 1 日的价格是 15,3 月 2 日是 16,3 月 3 日是 17...我尝试使用 group by 解决这个问题,但我仍然不是能够解决它...请任何人给我建议。
解决方案
如果你有所有日期的所有水果的价格,那么你可以这样做GROUP_CONCAT()
:
SELECT name
FROM tablename
GROUP BY name
HAVING COUNT(*) = COUNT(DISTINCT price) -- all prices are different
AND GROUP_CONCAT(price ORDER BY Date) = GROUP_CONCAT(price ORDER BY price)
-- prices ordered by date are in the same order as prices ordered ascending by price
请参阅演示。
推荐阅读
- image - 在 vue 应用程序上渲染之前降低图像质量
- amazon-web-services - 将用户数据传递给 EMR EC2 实例
- python - Python:分配基于循环值变化的灵活变量
- r - 以相同的顺序将所有逻辑规则与一个向量匹配
- python - 如何在循环中使用 for/
- reactjs - 如何使用样式化的组件为 Material UI 中的 Select 设置样式?
- python - 数字名词/数字数字名词的spacy匹配器
- typescript - 模板文字类型检查模板文字
- inheritance - 这仍然被认为是继承吗?这是一个好的编码实践吗?
- spring - PostMapping 和 DeleteMapping 的区别