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

mysql数据库搜索实例

时间:2018-06-27 21:02:48      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:esc   mysql   ima   tin   varchar   desc   ble   des   hda   

MySQL 数据库搜索实例练习(基础练习):

先再MySQL数据库中建两个表,实例如下:

CREATE TABLE student(
    stuNo CHAR(8) PRIMARY KEY,
    stuName VARCHAR(50) NOT NULL,
    stuage TINYINT NOT NULL,
    stuSex TINYINT,
    birthday DATETIME
);


INSERT INTO student VALUES(‘10011011‘,‘张三‘,19,1,‘1997-1-1 1:1:1‘);
INSERT INTO student VALUES(‘10011012‘,‘张小亮‘,20,0,‘1996-1-1 1:1:1‘);
INSERT INTO student VALUES(‘10011013‘,‘李四‘,16,0,‘1999-1-1 1:1:1‘);
INSERT INTO student VALUES(‘10011014‘,‘小李四‘,18,1,‘1998-1-1 1:1:1‘);
INSERT INTO student VALUES(‘10011015‘,‘小强‘,21,1,‘1994-1-1 1:1:1‘);
INSERT INTO student VALUES(‘10011016‘,‘赵玉‘,22,1,NULL);
INSERT INTO student VALUES(‘10011017‘,‘王五‘,25,1,‘1991-1-1 1:1:1‘);
INSERT INTO student VALUES(‘10011018‘,‘小六‘,17,0,‘2000-1-1 1:1:1‘);

CREATE TABLE stuscore(
    scoreNo CHAR(6) PRIMARY KEY,
    stuNo CHAR(8) NOT NULL,
    javaScore TINYINT NOT NULL,
    htmlScore TINYINT NOT NULL
);


INSERT INTO stuscore VALUES(‘200101‘,‘10011011‘,73,84);
INSERT INTO stuscore VALUES(‘200102‘,‘10011012‘,77,69);
INSERT INTO stuscore VALUES(‘200103‘,‘10011013‘,89,92);
INSERT INTO stuscore VALUES(‘200104‘,‘10011014‘,62,84);
INSERT INTO stuscore VALUES(‘200105‘,‘10011015‘,41,31);
INSERT INTO stuscore VALUES(‘200106‘,‘10011016‘,37,50);
INSERT INTO stuscore VALUES(‘200107‘,‘10011017‘,90,88);
INSERT INTO stuscore VALUES(‘200108‘,‘10011018‘,85,72);

(##数值时随意添加的)

搜索实例如下:

-- 查询所有学员信息
SELECT * FROM student;
-- 查询stuno为10011012的学生姓名
SELECT  stuname FROM  student WHERE stuno=‘10011012‘;
-- 查询stuno为10011014和10011016的学生信息
SELECT * FROM  student WHERE stuno=‘10011012‘ OR stuno=‘10011016‘;
-- 查询所有年龄在20岁以上的学生编号
SELECT stuno FROM student WHERE stuage>20;
-- 查询所有生日为空的学生信息
SELECT * FROM student WHERE birthday IS NULL;
-- 查询所有姓小的学生信息
SELECT * FROM student WHERE stuname LIKE‘小%‘;
-- 查询所有姓名中第二个字是小的学生信息
SELECT * FROM student WHERE stuname LIKE ‘_小%‘;
-- 查询所有姓名中带小的学员信息
SELECT * FROM student WHERE stuname LIKE ‘%小%‘
-- 查询所有姓小的名字为2个字的学生信息
SELECT * FROM student WHERE stuname LIKE ‘小_‘;
-- 查询所有学生姓名及年龄并按年龄升降序排列
SELECT stuname,stuage FROM student WHERE stuage ORDER BY stuage;
SELECT stuname,stuage FROM student WHERE stuage ORDER BY stuage DESC;
-- 查询所有姓小的学生信息按年龄降序排列
SELECT * FROM student WHERE stuname LIKE ‘小%‘ ORDER BY stuage;
-- 查找java成绩最高的学生编号
SELECT  stuNo FROM stuscore WHERE javaScore=(SELECT MAX(javaScore)FROM stuscore);
-- 查询所有html成绩在平均分以上的学生编号
SELECT  stuNo FROM stuscore WHERE htmlScore>(SELECT AVG(htmlScore)FROM stuscore);
-- 查询所有学生姓名、两科成绩
SELECT a.stuname,b.javaScore,b.htmlScore
FROM student a,stuscore b
WHERE a.stuNo=b.stuNo;
-- 查询java成绩最高的学生信息
SELECT * FROM student a,stuscore b
WHERE javaScore=(SELECT (MAX(javaScore)) FROM  stuscore)
AND a.`stuNo`=b.`stuNo`;
-- 查询所有java成绩大于html成绩的学生姓名
SELECT stuname FROM student a,stuscore b
WHERE b.`javaScore`>b.`htmlScore`
AND a.`stuNo`=b.`stuNo`;
-- 查询所有姓小的学生的姓名以及两科成绩并按年龄降序排列
SELECT a.stuname,b.javascore,b.htmlscore
FROM student a,stuscore b
WHERE a.`stuNo` = b.`stuNo

 

mysql数据库搜索实例

标签:esc   mysql   ima   tin   varchar   desc   ble   des   hda   

原文地址:https://www.cnblogs.com/zsc1/p/9235787.html

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