您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 莆田分类信息网,免费分类信息发布

sql数据库查询语句的基本语法是什么

2024/3/14 21:31:27发布20次查看
sql数据库查询语句的完整语法是“select [select选项] 字段列表[字段别名]/* from 数据源 [where 字句] [group by子句 ][having 子句][order by 子句][limit 子句];”。
本教程操作环境:windows7系统、mysql8版、dell g3电脑。
数据库是mysql,使用的数据库表名称是my_student.
表的完整数据信息是:
完整语法是:
select [select选项] 字段列表[字段别名]/* from 数据源 [where 字句] [group by子句 ][having 子句][order by 子句][limit 子句];
①[select选项]:
select 选项包含:all(所有,默认)、distinct(去重)。其中distinct针对的是查询结果的整条记录而言的。
select distinct(sex) from my_student;
select distinct(sex),name from my_student; 和 selectdistinct sex,name from my_student;结果是一样的。
②[where 字句]:where是唯一一个从磁盘开始拿数据的时候就开始进行判断的条件,从磁盘取出一条记录,开始进行where判断,判断结果如果成立,那么取出结果保存到内存,否则放弃。
select * from my_student where name = '哈哈1';
③[group by 子句 ]:分组子句,group by子句主要的作用是分组,从而进行统计操作,而不是为了展示(展示的时候,只会展示分组记录的第一条记录),分组时,一般会结合使用count()、max()、min()、avg()、sum()函数。
a、单子段分组:
selectc_id,count(*),max(height),min(height),avg(height),sum(age) from my_studentgroup by c_id ;
sql语句的意思是:my_student表以c_id进行分组,然后显示分组后的每组的c_id名称、每组的总数、每组的最高、最低、平均身高和每组的年龄总和。
b、多字段分组
select c_id,sex,count(*),max(height),min(height),avg(height),sum(age)from my_student group by c_id ,sex;
表示的含义是,对整个表先按照c_id进行分组,然后在此分组的基础之上,然后每组再按照sex,进行分组。
c、多字段分组(加上显示每组的某一字段的所有数据)
selectc_id,sex,count(*),max(height),min(height),avg(height),sum(age) ,group_concat(name)from my_student group by c_id ,sex;
④[having 子句]:having的作用类同where,而且having能做几乎所有where能做的事情,而where却不能做having能做的很多事情,主要是因为
where只能在磁盘提取数据的时候对数据进行操作;而在内存中对数据进行group by分组之后的结果进行处理,只能通过having。
selectc_id,count(*),max(height),min(height),avg(height),sum(age) from my_studentgroup by c_id having count(*) >= 3;
⑤[order by 子句]:对数据进行排序操作,根据某个字段进行升序或者降序排序。(进行多字段排序的时候,先根据某一字段进行潘旭,然后在排序好的内部再按照某字段进行排序)
a、单个字段的排序:
select * from my_student order by c_id;
b、多字段排序
select * from my_student order by c_id,sex;
⑥[limit 子句]:限制结果的数量。limit 偏移量  记录条数;
a、select * frommy_student limit 2;
b、select * frommy_student limit 0,3;
相关推荐:《mysql教程》
以上就是sql数据库查询语句的基本语法是什么的详细内容。
莆田分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录