mysql數據庫搜索實例

MySQL 數據庫搜索實例練習(基礎練習):html

先再MySQL數據庫中建兩個表,實例以下:java

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);

數據庫

(##數值時隨意添加的)spa

搜索實例以下:htm

-- 查詢全部學員信息
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
基礎

相關文章
相關標籤/搜索