有关MySQL查询语句的练习

  1. 先建立一个表,表的字段包括:no、name、sex、birthday、class
mysql> create table student(

    -> no varchar(20) primary key,

    -> name varchar(20) not null,

    -> sex varchar(20) not null,

    -> birthday datetime not null unique key,

    -> class varchar(20) not null);

Query OK, 0 rows affected (0.01 sec)

  1. 在该表中插入数据
mysql> insert student(no,name,sex,birthday,class) values('105','张三','女','1987-9-2 00:00:00','123');

Query OK, 1 row affected (0.00 sec)

【切记:如果再插入是无法识别中文则——sex names gbk;也是编码的问题】

  1. 以该表数据中的class字段降序输出
mysql> select * from student order by class desc;

  1. 列出该表中class字段不重复的部分(关键字:distinct)
mysql> select distinct class from student;

  1. 列出该表中的字段no、name、sex
mysql> select no,name,sex from student;

  1. 输出该表中不姓小的名字
mysql> select name from student where name not like '小%';

  1. 在该表中添加degree字段
mysql> alter table student add degree int not null after class;

  1. 给该字段下的每一条记录添加数据
mysql> update student set degree=degree+65 where no='105';

  1. 给该字段degree同时添加一个数字
mysql> update student set degree=degree+1;

  1. 输出成绩为99或者在60-70之间
mysql> select * from student where degree=99 or degree between 60 and 70;

  1. 输出班级为123或者性别为女的同学
mysql> select * from student where class='123' or sex='女';

  1. 输出一个班级为123或者性别为女的同学的姓名
mysql> select name from student where class='123' or sex='女';

  1. 以升序输出degree的所有记录
mysql> select * from student order by degree;

  1. 输出男生人数和男生所在班级的数量
mysql> select COUNT(*),COUNT(DISTINCT class) from student where sex='女';

  1. 列出存在85分以上的课程号
mysql> select distinct no from student where degree>85;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章