mysql======練習(1)

來源:互聯網
上載者:User

標籤:efault   address   prim   北京   插入資料   table   extra   總成績   51cto   

mysql 練習題


***建立student表

mysql> create table student (

    -> id int(10) not null unique primary key,

    -> name varchar(20) not null,

    -> sex varchar(4),

    -> brith year,

    -> deparment varchar(20),

    -> address varchar(50)

    -> );

Query OK, 0 rows affected (0.03 sec)


查看student表的結構

mysql> desc student

    -> ;

+-----------+-------------+------+-----+---------+-------+

| Field     | Type        | Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+-------+

| id       | int(10)     | NO   | PRI | NULL    |       |

| name     | varchar(20)  | NO   |     | NULL    |       |

| sex      | varchar(4)  | YES  |     | NULL    |       |

| brith     | year(4)    | YES  |     | NULL    |       |

| deparment | varchar(20)  | YES    |     | NULL    |       |

| address   | varchar(50)  | YES    |     | NULL    |       |

+-----------+-------------+------+-----+---------+-------+

6 rows in set (0.00 sec)


***建立score表

mysql> create table score (

    -> id int(10) not null unique primary key,

    -> stu_id varchar(20),

    -> c_name varchar(20),

    -> grade int(10)

    -> );

Query OK, 0 rows affected (0.02 sec)


查看score表的結構

mysql> desc score;

+--------+-------------+------+-----+---------+-------+

| Field  | Type        | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

| id     | int(10)     | NO   | PRI | NULL      |

| stu_id | varchar(20) | YES  |     | NULL    |       |

| c_name | varchar(20) | YES  |     | NULL    |       |

| grade  | int(10)     | YES  |    NULL        |

+--------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)


****向student表中插入資料

mysql> select * from student;

+-----+--------+------+-------+-----------+--------------+

| id  | name   | sex  | brith | deparment | address      |

+-----+--------+------+-------+-----------+--------------+

| 901 | 張老大 | 男   |  1985 | 電腦系  | 北京市海澱區 |

| 902 | 張老二 | 男   |  1987 | 電腦系  | 北京市昌平區 |

| 903 | 張三   | 女   |  1990 | 中文系    | 湖南省永州市 |

| 904 | 李四   | 男   |  1990 | 英語系    | 遼寧省阜新市 |

| 905 | 王五   | 女   |  1991 | 英語系    | 福建廈門市   |

| 906 | 王六   | 男   |  1998 | 電腦系  | 湖南省衡陽市 |

+-----+--------+------+-------+-----------+--------------+

6 rows in set (0.03 sec)


****向score表中插入資料

mysql> select * from score;

+----+--------+--------+-------+

| id | stu_id | c_name | grade |

+----+--------+--------+-------+

|  1 |    901 | 電腦 |    98 |

|  2 |    901 | 英語   |    80 |

|  3 |    902 | 電腦 |    65 |

|  4 |    902 | 中文   |    88 |

|  5 |    903 | 中文   |    95 |

|  6 |    904 | 電腦 |    70 |

|  7 |    904 | 英語   |    92 |

|  8 |    905 | 英語   |    90 |

+----+--------+--------+-------+

8 rows in set (0.01 sec)


==============================================================


****題目1

查詢student表中所有記錄


mysql> select * from student;


****題目2

查詢student表的第二條到第四條記錄

mysql> select * from student limit 1,3;


****題目3

從student表中查詢所有學生的學號(id)姓名(name)、和院系(deparment)的資訊.

mysql> select id,name,deparment from student;


****題目4

從student 表中查詢電腦系和英語系的學生資訊

mysql> select * from student where deparment=‘電腦系‘ or  deparment=‘英語系‘;


****題目5

從student表中查詢出生年齡在26-30歲的學生資訊

mysql> select id,name,sex,brith as age ,deparment,address

    -> from student

    -> where 2017-brith between 26 and 30;


****題目6

從student表中查詢每個院系有多少人

mysql> select deparment,count(id)

    -> from student

    -> group by deparment;


****題目7

從score表中查詢每個科目最高分

mysql> select c_name, max(grade)

    -> from score

    -> group by c_name;


****題目8

查詢李四的考試(c_name)科目和考試成績(grade)

mysql> select c_name,grade

    -> from score

    -> where stu_id=(

    -> select id from student

    -> where name=‘李四‘

    -> );


****題目9

用連結的形式查詢所有學生的資訊和考試成績

mysql> select student.id,name,sex,brith,deparment,address,c_name,grade

    -> from student, score

    -> where student.id=score.stu_id;


****題目10

計算每個學生的總成績降序排列

mysql> select student.id,name,sex,brith,deparment,address,sum(grade)

    -> from student,score

    -> where student.id=score.stu_id

    -> group by name

    -> order by sum(grade) desc;


****題目11

計算每個科目的平均成績

mysql> select student.id,name,sex,brith,deparment,address,avg(grade)

    -> from student,score

    -> where student.id=score.stu_id

    -> group by id;


****題目12

查詢電腦成績低於95的學生資訊

mysql> select * from student

    -> where id in (

    -> select stu_id from score

    -> where c_name=‘電腦‘ and grade<95);


****題目13

查詢同時參加電腦和英語考試的學生資訊

mysql> select * from student

    -> where id=any

    -> (select stu_id from score

    -> where stu_id in (

    -> select stu_id from

    -> score where c_name=‘電腦‘)

    -> and c_name=‘英語‘);


****題目14

從student表和score表查詢出學生的學號,然後合并查詢結果

mysql> select id from student

    -> union

    -> select stu_id from score;


****題目15

查詢姓張或者姓王的同學的姓名、院系和考試科目及成績

mysql> select student.id,name,sex,brith,deparment,address,c_name,grade

    -> from  student,score

    -> where (name like ‘張%‘ or name like ‘王%‘)

    -> and student.id=score.stu_id;


****題目16

查詢都是湖南的學生的姓名、年齡、院系和考試科目及成績

mysql> select student.id,name,sex,2017-brith as age,deparment,c_name,grade

    -> from student,score

    -> where address like ‘湖南%‘

    -> and

    -> student.id=score.stu_id;


本文出自 “IT生活” 部落格,請務必保留此出處http://dingxue.blog.51cto.com/12290895/1973552

mysql======練習(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.