有關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;

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