码迷,mamicode.com
首页 > 数据库 > 详细

mysql_05_单表查询

时间:2017-07-27 01:06:59      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:ima   信息   desc   tin   条件查询   select   mit   学生   auto   

#创建数据库
create database db_student;

#使用数据库
use db_student;

#创建表
create table `t_student` (
`id` int primary key not null auto_increment,
`stuName` varchar (60),
`age` int ,
`sex` varchar (30),
`gradeName` varchar (60)
);

#插入数据
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘1‘,‘张三‘,‘23‘,‘男‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘2‘,‘张三丰‘,‘25‘,‘男‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘3‘,‘李四‘,‘23‘,‘男‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘4‘,‘王五‘,‘22‘,‘男‘,‘三年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘5‘,‘珍妮‘,‘21‘,‘女‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘6‘,‘李娜‘,‘26‘,‘女‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘7‘,‘王峰‘,‘20‘,‘男‘,‘三年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘8‘,‘梦娜‘,‘21‘,‘女‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘9‘,‘小黑‘,‘22‘,‘男‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘10‘,‘追风‘,‘25‘,‘男‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘11‘,‘小小张三‘,‘21‘,NULL,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘12‘,‘小张三‘,‘23‘,‘男‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘13‘,‘张三锋小‘,‘24‘,NULL,‘二年级‘);

 

#第五章:查询数据
#第一节:单表查询
#5.1:查询所有字段
select id,stuName,age,sex,gradeName from t_student;
select stuName,gradeName,age,sex,id from t_student;
select * from t_student;

#5.2:查询指定字段
select stuName,gradeName from t_student;

#5.3:Where条件查询
select stuName,id from t_student where id=3;
select * from t_student where id=3;
select * from t_student where age>22;

#5.4:带in关键字查询
select * from t_student where age in (21,23);#查询age=21,或者age=23的学生的信息
select * from t_student where age not in (21,23);#查询age!=21,或者age!=23的学生的信息

#5.5:带between and 的范围查询
select * from t_student where age between 22 and 24;#查询age范围在(22,24)的学生的信息
select * from t_student where age not between 22 and 24;#查询age范围不在(22,24)的学生的信息

#5.6:带like的模糊查询,%:任意字符,_:单个字符
select * from t_student where stuName like "张%";#查询姓名为‘张‘字开头的信息
select * from t_student where stuName like "张_";#查询姓名为‘张‘字开头,并姓名只有两个字的信息
select * from t_student where stuName like "张__";#查询姓名为‘张‘字开头,并姓名只有三个字的信息
select * from t_student where stuName like "%张三%";#查询姓名包含张三的数据的信息

#5.7:空值查询
select * from t_student where sex is null;
select * from t_student where sex is not null;

#5.8:带and的多条件查询
select * from t_student where gradeName="一年级" and age=23;

#5.9:带or的多条件查询
select * from t_student where gradeName="一年级" or age=23;

#5.10:distinct去重复查询
select distinct gradeName from t_student;

#5.11:order by对查询结果排序
select * from t_student order by age;#默认升序排列
select * from t_student order by age asc;#年龄升序排列
select * from t_student order by age desc;#年龄降序排列

#5.12:group by分组查询
select gradeName,group_concat(stuName) from t_student group by gradeName;#按照年级进行分组,查询姓名
select gradeName,count(stuName) from t_student group by gradeName;#按照年级进行分组,查询各个年纪的人数
select gradeName,count(stuName) from t_student group by gradeName having count(stuName)>3;#对查询到的分组数据进行限制输出,只显示年级人数大于3的年级
select gradeName,count(stuName) from t_student group by gradeName with rollup;#多加一行显式总和
select gradeName,group_concat(stuName) from t_student group by gradeName with rollup;

#5.13:limit分页查询;limit 初始位置,记录数;
select * from t_student limit 0,5;
select * from t_student limit 6,5;
select * from t_student limit 10,5;

mysql_05_单表查询

标签:ima   信息   desc   tin   条件查询   select   mit   学生   auto   

原文地址:http://www.cnblogs.com/xinwenpiaoxue/p/7242502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!