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

MySQL(基础篇)之单表查询

2024/4/11 2:37:01发布19次查看
一:表的结构和数据 create table `t_student` ( `id` int primary key , `stuname` varchar (10) not null, `age` int not null , `sex` varchar (4) , `gradename` varchar (10) not null ); insert into `t_student` (`id`, `stuname`, `age`, `sex`, `gra

一:表的结构和数据
create table `t_student` (
`id` int primary key ,
`stuname` varchar (10) not null,
`age` int not null ,
`sex` varchar (4) ,
`gradename` varchar (10) not null
);
insert into `t_student` (`id`, `stuname`, `age`, `sex`, `gradename`) values('1','张三',23,'男','一年级'),('2','张三丰',25,'男','二年级'),('3','李四',23,'男','一年级'),
('4','王五',22,'男','三年级'),('5','珍妮',21,'女','一年级'),('6','李娜',26,'女','二年级'),('7','王峰',20,'男','三年级'),('8','黄志浩',21,'男','二年级'),
('9','杨磊',22,'男','一年级'),('10','邹涛',25,'男','二年级'),('11','鲍文杰',21,null,'二年级'),('12','朱云芬',23,'男','二年级'),('13','朱云峰','24',null,'二年级');
这是我为大家练习准备的数据,下面正式开始单表查询.
二:单表查询
2.1 查询所有字段
1.       select 字段1,字段2,字段3……from 表名;
例:select id,stuname,age,sex,gradename from t_student;
2.       select * from 表名;
例:select * from t_student;
两者区别:  前者可以通过改变查询字段的顺序来调整查询结果显示的顺序.
2.2 查询指定字段
select 字段1,字段2,字段3……from 表名;
例:/*查询t_student表的所有age,sex,gradename字段*/
select age,sex,gradename from t_student;
2.3 where条件查询
1.       select 字段1,字段2,字段3……from 表名 where 条件表达式;
例: 查询编号为1的学生
select * from t_student where id =1;
例: 查询年龄大于22的学生
select * from t_student where age>22;
例: 查询性别为男的学生
select * from t_student where sex='男';
2.4 带in关键字查询
1.       select 字段1,字段2,字段3……from 表名 where 条件表达式 [not] in(元素1,元素2,元素3);
例: 查询年龄为22或者23的学生记录
select * from t_student where age in (22,23);
例: 查询编号不为1和9的学生记录
select * from t_student where id not in(1,9);
2.5 带between的范围查询
1.       select 字段1,字段2,字段3……from 表名 where 条件表达式 [not] between 数值1 and 数值2;
例: 查询编号为1-9的学生记录
select * from t_student where id between 1 and 9;
例: 查询年龄小于22大于25的学生记录
select * from t_student where age not between 22 and 25;
2.6 带like的模糊查询
select 字段1,字段2,字段3……from 表名 where 条件表达式 [not] like ‘字符串’;
‘%’: 代表任意字符,可以有或者没有
‘_’: 代表单个字符,必须要有
例: 查询姓名中包含张的学生记录
select * from t_student where stuname like '%张%';
例: 查询姓名中第一个字为张的学生记录
select * from t_student where stuname like '张%';
例: 查询姓名中第一个字为张,并且姓名只有2个字的学生记录
select * from t_student where stuname like '张_';
2.7 空值查询
select 字段1,字段2,字段3……from 表名 where 条件表达式 is[not] null;
例: 查询性别为null的学生记录
select * from t_student where sex is null;
例: 查询性别不为null的学生记录
select * from t_student where sex is not null;
2.8     带and的多条件查询
select 字段1,字段2,字段3……from 表名 where 条件表达式1 and 条件表达式2[...and 条件表达式n];
例: 查询一年级中性别为男的学生记录
select * from t_student where gradename='一年级' and sex='男';
例: 查询二年级中年龄为22-24的学生记录
select * from t_student where gradename='二年级' and age between 22 and 24;
2.9 带or的多条件查询
select 字段1,字段2,字段3……from 表名 where 条件表达式1 or 条件表达式2[...or 条件表达式n];
例: 查询年级为一年级或者年龄为23的学生记录
select * from t_student where gradename='一年级' or age=23;
2.10  distinct去重复查询
select distinct 字段名 from 表名;
例子: 查询学生表所有的年级
select distinct gradename from t_student ;
不加distinct会有很多重复记录
2.11对查询结果进行排序
select 字段1,字段2,字段3……from 表名order by 属性名 [asc|desc]
例: 查询所有学生记录并按年龄降序排序
select * from t_student order by age desc;
2.12 group by 分组查询
select 字段1,字段2,字段3……from 表名 group by属性名;[having 条件表达式][with rollup];
1.       单独使用(毫无意义);
2.       与group_concat()函数一起使用;
3.       与聚合函数一起使用;
4.       与having一起使用(对查询结果的筛选);
5.       与with rollup一起使用(最后加入一个总和行);
例: 查询每个年级的所有学生姓名
select gradename,group_concat(stuname) from t_student group by gradename;
例: 查询每个年级的学生个数
select count(stuname) as '学生数量',gradename as '年级' from t_student group by gradename;
例: 查询每个年级的学生个数并且选出学生数量大于3的年级
select count(stuname) as '学生数量',gradename as '年级' from t_student group by gradename having count(stuname)>3;
例: 查询每个年级的学生个数并在末尾加入一个总和行
select count(stuname) as '学生数量',gradename as '年级' from t_student group by gradename with rollup;
2.13 limit 分页查询
select 字段1,字段2,字段3……from 表名 limit 初始位置,记录数;
例: 查询所有学生记录的前5条记录
select * from t_student limit 0,5;
三:总结
单表查询就到此结束,大家要多多练习,谢谢大家,下期为大家带来聚合函数的使用.
该用户其它信息

VIP推荐

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