Java学习-MySQL-DQL数据查询-模糊查询操作符
模糊查询操作符
运算符 |
语法 |
描述 |
IS NULL |
|
数据为空 |
IS NOT NULL |
|
数据不为空 |
BETWEEN b AND c |
a between b and c |
数据在a到b之间 |
Like |
a like b |
a匹配b |
In |
a in (a1,a2,a3,…) |
a在集合内 |
初始化
CREATE DATABASE IF NOT EXISTS `school`;
USE `school`;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`(
`studentno` INT(4) NOT NULL COMMENT '学号',
`studentname` VARCHAR(20) DEFAULT NULL COMMENT '学生姓名',
`sex` TINYINT(1) DEFAULT NULL COMMENT '性别,0或1',
`gradeid` INT(11) DEFAULT NULL COMMENT '年级编号',
`address` VARCHAR(255) NOT NULL COMMENT '地址,允许为空',
PRIMARY KEY (`studentno`)
)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;
INSERT INTO `student` (`studentno`,`studentname`,`sex`,`gradeid`,`address`)
VALUES
(1000,'赵云',1,1,'北京朝阳'),
(1001,'刘禅',1,1,'河南洛阳'),
(1002,'诸葛亮',1,2,'天津红桥'),
(1003,'曹操',1,3,'广西桂林'),
(1004,'刘备',1,3,'云南大理'),
(1005,'关羽',1,3,'新疆乌鲁木齐'),
(1006,'张飞',1,3,'四川成都'),
(1007,'孙策',1,4,'北京海淀'),
(1008,'刘邦',1,4,'北京东城'),
(1009,'韩信',1,4,'北京西城'),
(1010,'刘星',1,4,'北京西城'),
(1011,'刘星星',1,4,'北京西城'),
(1012,'刘六留',1,4,'北京西城');
INSERT INTO `result`(`studentno`,`subjectno`,`examdate`,`studentresult`)
VALUES
(1000,1,'2013-11-11 16:00:00',85),
(1000,3,'2013-11-12 16:00:00',87),
(1001,1,'2013-11-11 16:00:00',78),
(1001,3,'2013-11-12 16:00:00',88),
(1002,2,'2013-11-11 09:00:00',88),
(1002,4,'2013-11-12 09:00:00',96),
(1002,5,'2013-11-13 09:00:00',92),
(1003,6,'2013-11-11 16:00:00',75),
(1003,7,'2013-11-12 16:00:00',88),
(1004,6,'2013-11-11 16:00:00',66),
(1004,7,'2013-11-12 16:00:00',62),
(1005,6,'2013-11-11 16:00:00',74),
(1005,7,'2013-11-12 16:00:00',68),
(1006,6,'2013-11-11 16:00:00',84),
(1006,7,'2013-11-12 16:00:00',83),
(1007,8,'2013-11-13 16:00:00',66),
(1008,8,'2013-11-13 16:00:00',67),
(1009,8,'2013-11-13 16:00:00',68);
INSERT INTO `grade` (`gradeid`,`gradename`) VALUES(1,'大一'),(2,'大二'),(3,'大三'),(4,'大四');
INSERT INTO `subject`(`subjectno`,`subjectname`,`classhour`,`gradeid`)VALUES
(1,'高等数学-1',110,1),
(2,'高等数学-2',110,2),
(3,'C语言-1',110,1),
(4,'C语言-2',110,2),
(5,'Java程序设计-2',110,2),
(6,'Java程序设计-3',110,3),
(7,'数据库结构-3',110,3),
(8,'数据库结构-4',110,4);
查询姓刘的同学
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 '%星%';
查询在北京的学生
SELECT StudentNo,StudentName,address FROM student
WHERE address LIKE '%北京%';
查询1001~1004号学生
SELECT StudentNo,StudentName FROM student
WHERE studentno IN (1001,1002,1003,1004);
查询地址不为空的学生
SELECT StudentNo,StudentName,address FROM student
WHERE address IS NOT NULL