39-数据库5【常用的SQL查询语句】

一、查询一个表中的所有内容

select * from 表名称;

示例:查询学生信息表的所有信息

select * from studentInfo;


二、查询表中指定字段的内容

select 列名称 from 表名称;

示例:查询一个表中的所有学生的名字

select name from studentInfo;


三、根据条件查询

select * from 表名称 where 列名称='条件';

示例:查询名为“李东晓”的所有信息

select * from studentInfo where name='李东晓';


select 列名称1,列名称2,列名称3... from 表名称where 列名称='条件';

示例:查询名为“李东晓”的年龄和性别

select age,sex from studentInfo where name='李东晓';


四、根据条件模糊查询

select * from 表名称 where 列名称 like'条件%';

示例:查询表中姓“李”的学生的所有信息

select * from studentInfo where name like'李%';


select * from 表名称 where 列名称 like'%条件%';

示例:查询表中名字包含“一”的所有学生信息

select * from studentInfo where name like'%_%';

select * from 表名称 where 列名称 like'_条件'(_表示替代一个字符);

示例:查询表中最后以山结尾三个字的所有学生信息

select * from studentInfo where name like'__山';


五、根据条件去除重复内容

select distinct  需要去重的列名称 from 表名称;

示例:查询表中的所有学生的年龄段

select distinct age from studentInfo;


六、查询指定区间内的内容

select *  from表名称 where 需要指定区间的列名称 between '条件1' and '条件2';

示例:查询表中年龄在20到30岁的所有学生信息

select *  fromstudentInfo where age between '20' and '30';


七、根据条件进行排序

select *  from表名称 order by 需要排序的列名称 排序关键字(asc或desc[asc是按照从小到大排序;desc是按照从大到小排序的]);

示例:按照学生年龄从小到大排序

select *  fromstudentInfo order by age asc;


八、根据条件求平均数

select avg(需要求平均数的列名称)  from表名称;

示例:查询表中学生的平均年龄

select avg(age) from studentInfo;


九、根据条件查询对应内容的数量

1-查询表中的所有数目

select count(*) as numbers  from表名称;

示例:统计当前表中的所有学生数目

select count(*) as numbers  from studentInfo;


2-根据条件查询对应的数目

select count(*) as numbers from 表名称 where 需要查询数目的列名称='条件';

示例:查询表中性别为“女”的所有学生的数目

select count(*) as numbers from studentInfo where sex='女';


十、根据条件求和

select sum(需要求和的列名称) as TotalNumbers from 表名称;

示例:查询表中所有学生的年龄加起来有多大

select sum(age) as TotalNumbers from studentInfo;


十一、求最大最小值

select 最大最小关键字[max是最大min是最小](需要求最大最小值的列名称) as value from 表名称;

示例:查询表中年龄最大的学生是几岁

select max(age) as value from studentInfo;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容