当前位置:网站首页>问题记录与思考
问题记录与思考
2022-06-25 21:57:00 【newProxyInstance】
前言:日常的问题记录和思考。
Mysql Group by 语句
学习group by首先要知道 聚合函数是什么? 有哪些聚合函数?
最常见的用法,查询每种商品的最低规格。
SELECT
goods_id,
min( goods_price )
FROM
tb_sku_info
GROUP BY
goods_id
但是我们可能需要其他的字段比如id或者name等等,查询非聚合列的时候可能会报错,例如下面的语句。
SELECT
id,
goods_id,
min( goods_price )
FROM
tb_sku_info
GROUP BY
goods_id
查询 sql_mode 语句
select @@sql_mode
set @@sql_mode = ONLY_FULL_GROUP_BY
select * from config_info group by group_id
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
'nacos_config.config_info.id' which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
去除sql_mode中ONLY_FULL_GROUP_BY这一规范是不正确的,不注意的话会带来其他的问题。
最显而易见的是 错行的 问题 。
SELECT
id,
specifications,
goods_id,
min( goods_price )
FROM
tb_sku_info
GROUP BY
goods_id
查询结果如图所示

查询总表后可以看到红框内的价格是商品最低的价格。

正确的写法,通过一个链接查询,用 最低价格跟 id 两个 字段来唯一匹配主表中的行记录即可。
SELECT
*
FROM
tb_sku_info AS t1
INNER JOIN ( SELECT goods_id, min( goods_price ) AS mixPrice FROM tb_sku_info GROUP BY goods_id ) AS t2 ON t1.goods_id = t2.goods_id
AND t1.goods_price = t2.mixPrice

边栏推荐
- Es7/es9 -- new features and regularities
- Fastjson反序列化随机性失败
- Thinking while walking
- 使用EAST ocr遇到的坑解决方法(编译lanms)
- Relinearization in homomorphic encryption (ckks)
- Record the learning record of the exists keyword once
- 异或运算符简单逻辑运算 a^=b
- ES6 - numerical extension and object extension
- Unity技术手册 - 生命周期内颜色ColorOverLifetime--速度颜色ColorBySpeed
- LM小型可编程控制器软件(基于CoDeSys)笔记十七:pto脉冲功能块
猜你喜欢

1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil

oracle -- 表操作

2022-2028 global open source cloud storage industry research and trend analysis report

Use apiccloud AVM multi terminal component to quickly realize the search function in the app

作为一个程序员我们如何快乐的学习成长进步呢?(个人感悟和技术无关)

Es7/es9 -- new features and regularities

LM小型可编程控制器软件(基于CoDeSys)笔记十七:pto脉冲功能块

2022爱分析· IT运维厂商全景报告

How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform

元宇宙标准论坛成立
随机推荐
2. What is the geometric meaning of a vector multiplying its transpose?
No absurd tea applet - rule change
多台云服务器的 Kubernetes 集群搭建
使用EAST ocr遇到的坑解决方法(编译lanms)
ORACLE - 数据查询
Civil Aviation Administration: by 2025, China will initially build a safe, intelligent, efficient and green aviation logistics system
Flex & Bison 開始
Transformers load pre training model
Record the learning record of the exists keyword once
建立自己的网站(15)
万亿热钱砸向太空经济,真的是一门好生意?
2022年中职组网络安全新赛题
Determine whether the appointment time has expired
New network security competition of the secondary vocational group in 2022
Common MySQL database functions and queries
LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
Unity technical manual - getKey and getaxis and getbutton
Some points to pay attention to when closing mongodb services (as well as related commands when opening)
pdm的皮毛
LM小型可编程控制器软件(基于CoDeSys)笔记十七:pto脉冲功能块