Mysql中错误使用SQL语句Groupby被兼容的情况
作者:mrr 时间:2024-01-16 03:55:10
首先创建数据库hncu,建立stud表格。
添加数据:
create table stud(
sno varchar(30) not null primary key,
sname varchar(30) not null,
age int,
saddress varchar(30)
);
INSERT INTO stud VALUES('1001','Tom',22,'湖南益阳');
INSERT INTO stud VALUES('1002','Jack',23,'益阳');
INSERT INTO stud VALUES('1003','李白',22,'益阳');
INSERT INTO stud VALUES('1004','王五',24,'中国北京');
INSERT INTO stud VALUES('1005','张三',22,'益阳');
INSERT INTO stud VALUES('1006','张四',23,'益阳');
INSERT INTO stud VALUES('1007','李四',22,'湖南益阳');
INSERT INTO stud VALUES('1008','刘备',24,'北京');
执行语句如下:
<喎�"/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:sql;"> SELECT * FROM stud GROUP BY saddress;
显示了如下错误:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hncu.stud.sno' which is not functionally dependent
on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
再执行此句:
SELECT saddress as 平均年龄 FROM stud GROUP BY saddress;
-没有问题
然后我们用MySQL,再执行前面那句错误的代码:
也就是:
SELECT * FROM stud GROUP BY saddress;
我们看结果:
顺利的通过了,但是,你发现没有,前面的smo,sname,age,这3列的数据不对啊,没错,MySQL强行显示第一次查找到的saddress不同的行了!!!其实这个结果是不对,但是MySQL应该是兼容了这个错误!
而DOS却是严格按照SQL的语法来的。
SQL的grop by 语法为,select 选取分组中的列+聚合函数 from 表名称 group by 分组的列
从语法格式来看,是先有分组,再确定检索的列,检索的列只能在参加分组的列中选。
所以问题中的,group by 后的 a,b,c是先确定的。select后的a,b,c才是可以变的。即
以下语句都是正确的:
select a,b,c from table_name group by a,b,c,d;
select a,b from table_name group by a,b,c;
select a,max(a) from table_name group by a,b,c;
以下语句则是错误的:
select a,b,c from table_name group by a,b;
select a,b,c from table_name group by a;
而因为MySQL的强大,它兼容了这个错误!!!
但是在DOS是不能的。所以出现了DOS下报错,而在MySQL中能够查找的情况(其实这个查找的结果是不对的)。
以上所述是小编给大家介绍的SQL语句Groupby在MySQL中错误使用被兼容的情况,网站的支持!
标签:sql,groupby
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现简单颜色识别程序
2022-07-15 09:34:47
![](https://img.aspxhome.com/file/2023/4/121094_0s.jpg)
Python3 pyecharts生成Html文件柱状图及折线图代码实例
2022-03-17 01:01:46
![](https://img.aspxhome.com/file/2023/1/84031_0s.png)
使用tensorflow实现矩阵分解方式
2022-04-17 17:01:17
![](https://img.aspxhome.com/file/2023/7/89347_0s.jpg)
tensorflow实现打印ckpt模型保存下的变量名称及变量值
2021-07-02 21:19:25
使用python创建生成动态链接库dll的方法
2021-02-13 22:18:21
numpy使用fromstring创建矩阵的实例
2022-02-23 09:29:49
python中的psutil模块详解(cpu、内存、磁盘情况、结束指定进程)
2021-12-19 23:33:04
Python Series从0开始索引的方法
2021-07-17 19:20:38
Python实现二分法算法实例
2021-06-23 19:31:12
手把手教你使用Python创建微信机器人
2022-02-11 14:12:29
![](https://img.aspxhome.com/file/2023/4/64724_0s.jpg)
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
2023-07-23 12:51:08
![](https://img.aspxhome.com/file/2023/7/64327_0s.png)
关于使用python对mongo多线程更新数据
2021-08-22 22:07:12
python使用wxPython打开并播放wav文件的方法
2023-08-23 10:30:58
谈谈图片如何影响转换率
2011-08-10 19:14:08
Pytorch实现LSTM和GRU示例
2022-02-08 09:14:44
![](https://img.aspxhome.com/file/2023/0/121510_0s.jpg)
Python实现简单2048小游戏
2023-08-13 00:19:18
![](https://img.aspxhome.com/file/2023/3/71553_0s.jpg)
Go中变量命名规则与实例
2024-05-09 09:55:45
Python实现人机中国象棋游戏
2023-01-28 21:33:52
![](https://img.aspxhome.com/file/2023/9/133489_0s.jpg)
全面解析Bootstrap表单使用方法(表单按钮)
2024-05-10 14:08:49
![](https://img.aspxhome.com/file/2023/9/126809_0s.jpg)
django生产环境搭建(uWSGI+django+nginx+python+MySQL)
2024-01-26 03:27:48
![](https://img.aspxhome.com/file/2023/6/120976_0s.png)