1. 程式人生 > >mysql練習題二

mysql練習題二

技術 between 成績 mit com pan birt pri 信息

技術分享

建立表格

1.創建studentscore

CREATE TABLE student (

id INT(10) NOT NULL UNIQUE PRIMARY KEY ,

name VARCHAR(20) NOT NULL ,

sex VARCHAR(4) ,

birth YEAR,

department VARCHAR(20) ,

address VARCHAR(50)

);

創建score表。SQL代碼如下:

CREATE TABLE score (

id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,

stu_id INT(10) NOT NULL ,

c_name VARCHAR(20) ,

grade INT(10)

);

2.為student表和score表增加記錄

student表插入記錄的INSERT語句如下:

INSERT INTO student VALUES( 901,‘張老大‘, ‘‘,1985,‘計算機系‘, ‘北京市海澱區‘);

INSERT INTO student VALUES( 902,‘張老二‘, ‘‘,1986,‘中文系‘, ‘北京市昌平區‘);

INSERT INTO student VALUES( 903,‘張三‘, ‘‘,1990,‘中文系‘, ‘湖南省永州市

‘);

INSERT INTO student VALUES( 904,‘李四‘, ‘‘,1990,‘英語系‘, ‘遼寧省阜新市‘);

INSERT INTO student VALUES( 905,‘王五‘, ‘‘,1991,‘英語系‘, ‘福建省廈門市‘);

INSERT INTO student VALUES( 906,‘王六‘, ‘‘,1988,‘計算機系‘, ‘湖南省衡陽市‘);

score表插入記錄的INSERT語句如下:

INSERT INTO score VALUES(NULL,901, ‘計算機‘,98);

INSERT INTO score VALUES(NULL,901, ‘英語

‘, 80);

INSERT INTO score VALUES(NULL,902, ‘計算機‘,65);

INSERT INTO score VALUES(NULL,902, ‘中文‘,88);

INSERT INTO score VALUES(NULL,903, ‘中文‘,95);

INSERT INTO score VALUES(NULL,904, ‘計算機‘,70);

INSERT INTO score VALUES(NULL,904, ‘英語‘,92);

INSERT INTO score VALUES(NULL,905, ‘英語‘,94);

INSERT INTO score VALUES(NULL,906, ‘計算機‘,90);

INSERT INTO score VALUES(NULL,906, ‘英語‘,85);

二:查詢訓練

-- 查詢student表的第2條到4條記錄
SELECT * FROM student LIMIT 1,3;

-- 從student表查詢所有學生的學號(id)、姓名(name)和院系(department)的信息
SELECT id,name,department FROM student;

-- 6.從student表中查詢計算機系和英語系的學生的信息
select * FROM student WHERE department in (‘計算機系‘,‘英語系‘);

-- 7.從student表中查詢年齡20~30歲的學生信息
select * FROM student WHERE year(NOW())-birth BETWEEN 20 and 30;

-- 8.從student表中查詢每個院系有多少人
SELECT department,COUNT(department) as ‘人數‘ FROM student GROUP BY department;

-- 9.從score表中查詢每個科目的最高分
SELECT c_name,MAX(grade) as ‘最高分‘ FROM score GROUP BY c_name;

-- 10.查詢李四的考試科目(c_name)和考試成績(grade)
SELECT * FROM score WHERE stu_id=(SELECT id FROM student WHERE name=‘李四‘);

-- 11.用連接的方式查詢所有學生的信息和考試信息
SELECT * FROM score,student WHERE stu_id=student.id;

-- 12.計算每個學生的總成績
SELECT stu_id,name,sum(grade)AS ‘總成績‘ FROM score,student WHERE student.id=stu_id GROUP BY stu_id;
-- 13.計算每個考試科目的平均成績
SELECT c_name,AVG(grade) FROM score GROUP BY c_name;

-- 14.查詢計算機成績低於95的學生信息
SELECT * FROM student WHERE id in
(select stu_id FROM score WHERE grade<95 and c_name=‘計算機‘);

-- 15.查詢同時參加計算機和英語考試的學生的信息
SELECT * FROM student WHERE id in
(SELECT a.stu_id FROM (SELECT * FROM score WHERE c_name=‘計算機‘)a,
(SELECT * FROM score WHERE c_name=‘英語‘)b WHERE a.stu_id=b.stu_id);


-- 16.將計算機考試成績按從高到低進行排序
SELECT c_name,grade FROM score WHERE c_name=‘計算機‘ ORDER BY grade;

-- 17.從student表和score表中查詢出學生的學號,然後合並查詢結果
SELECT DISTINCT student.id FROM student JOIN score WHERE stu_id=student.id;

-- 18.查詢姓張或者姓王的同學的姓名、院系和考試科目及成績
SELECT name,department,c_name,grade FROM score,student WHERE (name LIKE ‘張%‘ OR name LIKE ‘王%‘) AND student.id=stu_id;

SELECT name,department,c_name,grade FROM score,
(SELECT * FROM student WHERE name LIKE ‘張%‘ OR name like ‘王%‘)a
WHERE stu_id=a.id;
-- 19.查詢都是湖南的學生的姓名、年齡、院系和考試科目及成績
SELECT name,YEAR(NOW())-birth as ‘年齡‘ ,department,c_name,grade
FROM score,(SELECT * FROM student WHERE address LIKE ‘湖南%‘)a where a.id=stu_id;

mysql練習題二