Mysql基础:(DOL)基本的select语句使用(重要)
1.1创建一个含有基本信息的数据库
create database if not exists `school`; -- 创建一个school数据库 use `school`;-- 创建学生表 drop table if exists `student`; create table `student`( `studentno` int(4) not null comment '学号', `loginpwd` varchar(20) default null, `studentname` varchar(20) default null comment '学生姓名', `sex` tinyint(1) default null comment '性别,0或1', `gradeid` int(11) default null comment '年级编号', `phone` varchar(50) not null comment '联系电话,允许为空', `address` varchar(255) not null comment '地址,允许为空', `borndate` datetime default null comment '出生时间', `email` varchar (50) not null comment '邮箱账号允许为空', `identitycard` varchar(18) default null comment '身份证号', primary key (`studentno`), unique key `identitycard`(`identitycard`), key `email` (`email`) )engine=myisam default charset=utf8; -- 创建年级表 drop table if exists `grade`; create table `grade`( `gradeid` int(11) not null auto_increment comment '年级编号', `gradename` varchar(50) not null comment '年级名称', primary key (`gradeid`) ) engine=innodb auto_increment = 6 default charset = utf8; -- 创建科目表 drop table if exists `subject`; create table `subject`( `subjectno`int(11) not null auto_increment comment '课程编号', `subjectname` varchar(50) default null comment '课程名称', `classhour` int(4) default null comment '学时', `gradeid` int(4) default null comment '年级编号', primary key (`subjectno`) )engine = innodb auto_increment = 19 default charset = utf8; -- 创建成绩表 drop table if exists `result`; create table `result`( `studentno` int(4) not null comment '学号', `subjectno` int(4) not null comment '课程编号', `examdate` datetime not null comment '考试日期', `studentresult` int (4) not null comment '考试成绩', key `subjectno` (`subjectno`) )engine = innodb default charset = utf8; -- 插入学生数据 其余自行添加 这里只添加了2行 insert into `student` (`studentno`,`loginpwd`,`studentname`,`sex`,`gradeid`,`phone`,`address`,`borndate`,`email`,`identitycard`) values (1000,'123456','张伟',0,2,'13800001234','北京朝阳','1980-1-1','text123@qq.com','123456198001011234'), (1001,'123456','赵强',1,3,'13800002222','广东深圳','1990-1-1','text111@qq.com','123456199001011233'); -- 插入成绩数据 这里仅插入了一组,其余自行添加 insert into `result`(`studentno`,`subjectno`,`examdate`,`studentresult`) values (1000,1,'2013-11-11 16:00:00',85), (1000,2,'2013-11-12 16:00:00',70), (1000,3,'2013-11-11 09:00:00',68), (1000,4,'2013-11-13 16:00:00',98), (1000,5,'2013-11-14 16:00:00',58); -- 插入年级数据 insert into `grade` (`gradeid`,`gradename`) values(1,'大一'),(2,'大二'),(3,'大三'),(4,'大四'),(5,'预科班'); -- 插入科目数据 insert into `subject`(`subjectno`,`subjectname`,`classhour`,`gradeid`)values (1,'高等数学-1',110,1), (2,'高等数学-2',110,2), (3,'高等数学-3',100,3), (4,'高等数学-4',130,4), (5,'C语言-1',110,1), (6,'C语言-2',110,2), (7,'C语言-3',100,3), (8,'C语言-4',130,4), (9,'Java程序设计-1',110,1), (10,'Java程序设计-2',110,2), (11,'Java程序设计-3',100,3), (12,'Java程序设计-4',130,4), (13,'数据库结构-1',110,1), (14,'数据库结构-2',110,2), (15,'数据库结构-3',100,3), (16,'数据库结构-4',130,4), (17,'C#基础',130,1);
1.2指定字段查询(简单查询)
- 1.指定查询字段 -- 查询表中所有信息 SELECT * from student -- 查询指定字段 SELECT `studentno`,`studentname` FROM student -- 别名,给结果起一个名字 AS 可以给字段起别名,也可以给表起别名 SELECT `studentno` AS 学号, `studentname` AS 学生姓名 FROM student AS s -- 函数 Concat(a,b) SELECT CONCAT('姓名:',studentname) AS 新名字 FROM student -- 语法:select 字段...from 表
1.3 去重(districnt)
SELECT * from result -- 查询那些同学参加了考试,有成绩 SELECT `studentno` FROM result -- 发现重复数据去重,去重(distinct) SELECT distinct `studentno` FROM result
1.4 select 用来计算(表达式)
SELECT VERSION() -- 查询系统版本(函数) -- concat函数,定义行别名 SELECT concat('值:',100-8) AS 计算结果 -- 用来计算(表达式) SELECT @@auto_increment_increment -- 查询自增步长(变量)-- -- 学员考试成绩 +1分查看 SELECT `studentno`,`studentresult`+1 AS '提分后' FROM result
1.5 where子句,逻辑运算符
-- where 条件子句 SELECT studentno,studentresult from result -- 查询考试成绩在95~100之间的 SELECT studentno,studentresult FROM result WHERE studentresult>=95 and studentresult<=100 -- 模糊查询(区间) SELECT studentno,studentresult FROM result WHERE studentresult between 95 AND 100 -- 查询1000号学生之外的同学的成绩 -- != SELECT studentno,studentresult FROM result WHERE studentno !=1000 -- NOT SELECT studentno,studentresult FROM result WHERE NOT studentno=1000
1.6模糊查询操作符详解(本质,比较运算符)
运算符 | 语法 | 描述 |
is null | a is null | 如果操作符为null,结果为真 |
is not null | a is not null | 如果操作符不为null,结果为真 |
between | a between b and c | 若a在b和c之间,则结果为真 |
like | a like b | SQL匹配,如果a匹配b,则结果为真 |
in | a in(a1,a2,a3..) | 假设a在a1,或者a2...其中的某一个值中,则结果为真 |
1.6.1 like 结合% 和 _
-- 模糊查询 SELECT * FROM student INSERT INTO student (studentno,loginpwd,studentname,sex,gradeid,phone,address,borndate,email,identitycard) VALUES(1003,'122223','赵四',1,2,'13800001222','上海','1880-1-2','@qq.com','1234567890'); -- 查询姓张的同学 -- like结合 %(代表0的任意个数) _(一个字符) -- 查询姓张的同学 SELECT studentno,studentname FROM student WHERE studentname like '张%' -- 查询姓刘的同学,且名只有一个字 SELECT studentno,studentname FROM student WHERE studentname like '张_' -- 查询姓刘的同学,且名只有两个字 SELECT studentno,studentname FROM student WHERE studentname like '张__' -- 查询姓名字中间有强字同学, SELECT studentno,studentname FROM student WHERE studentname like '%强%'
-- 注% _通配符只能在like中用
1.6.2 in
-- in(具体的一个或者多个值) --查询1001,1003号学员 SELECT studentno,studentname FROM student WHERE studentno IN(1001,1003)
1.6.3 null not null
-- null not null \ -- 查询学生地址为空的同学 SELECT studentno,studentname from student where address=''or address is null -- 查询有出生日期的同学 SELECT studentno,studentname from student where borndate is not NULL