mysql查询学习笔记【一】

查询的数据在之前的一篇博客中,上连接: 数据准备

首先学习第一个查询:

一.以students表为例,查询表中所有的记录

select * from students;

结果如下:

+-----+--------+------+---------------------+-------+
| sno | sname  | ssex | sbirthday           | class |
+-----+--------+------+---------------------+-------+
| 101 | 曾华   || 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明   || 1975-10-02 00:00:00 | 95031 |
| 103 | 王丽   || 1976-01-23 00:00:00 | 95033 |
| 104 | 李军   || 1975-02-10 00:00:00 | 95033 |
| 105 | 王芳   || 1974-06-03 00:00:00 | 95031 |
| 106 | 陆君   || 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼玛 || 1976-02-20 00:00:00 | 95033 |
| 108 | 张全蛋 || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱 || 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+

这个* 的意思就是显示全字段,也就是最上面那一行,如果我们只想查询其中的几个字段时:
二.查询students表中sname,ssex,class列

select sname,ssex,class from students;

结果如下:

+--------+------+-------+
| sname  | ssex | class |
+--------+------+-------+
| 曾华   || 95033 |
| 匡明   || 95031 |
| 王丽   || 95033 |
| 李军   || 95033 |
| 王芳   || 95031 |
| 陆君   || 95031 |
| 王尼玛 || 95033 |
| 张全蛋 || 95031 |
| 赵铁柱 || 95031 |
+--------+------+-------+

也就是我们把我们想要显示的列写在select后面,如果想显示全部就写*号。
这时候如果我们想查询教师的所在部门的列,我们看一下结果:

mysql> select depart from teacher;
+------------+
| depart     |
+------------+
| 计算机系   |
| 计算机系   |
| 电子工程系 |
| 电子工程系 |
+------------+

我们得到了上面的表,如果我们的教师很多的话,就会有很多很多重复的,如果我们想去重的话应该怎么做呢?
三.查询教师表中所有所在部门即depart不重复的列
这时候就使用到了一个关键字叫做distinct

select distinct depart from teacher;

我们来看一下结果:

mysql> select distinct depart from teacher;
+------------+
| depart     |
+------------+
| 计算机系   |
| 电子工程系 |
+------------+

现在我们就把之前重复的记录给去重了,所以如果我们在查询中想要去重的话,就添加distinct关键字。

然后我们再学习一下区间查询:
四.查询score表中成绩在60-80之间的记录
查询区间使用到了between…and…关键字,具体用法:

select * from score where degree between 60 and 80;

结果如下:

mysql> select * from score where degree between 60 and 80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
+-----+-------+--------+

也可以使用运算符和并且:

select * from score where degree>60 and degree<80;

结果如下:

mysql> select * from score where degree>60 and degree<80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
+-----+-------+--------+

这里使用到了and也就是并且的意思,和&&是一样的,where大概是筛选的意思(我理解的)
既然提到了并且,就肯定也少不了或者:
五.查询score表中成绩为85或者86或者88的记录
表示或者的查询要使用关键字in,具体方法:

select * from score where degree in (85,86,88);

结果如下:

+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
+-----+-------+--------+

那如果要查询:
六.student表中‘95031’班级或者性别为‘女’的记录要怎么办呢?
我们刚刚的后者查询是在同一个字段中的或者查询,如果是不同字段之间的或者查询应该怎么办呢?
这时候我需要使用到or关键字,表示或者,具体方法如下:

select * from students where class=‘95031’ or ssex=‘女’;

结果如下:

mysql> select * from students where class='95031' or ssex='女';
+-----+--------+------+---------------------+-------+
| sno | sname  | ssex | sbirthday           | class |
+-----+--------+------+---------------------+-------+
| 102 | 匡明   || 1975-10-02 00:00:00 | 95031 |
| 103 | 王丽   || 1976-01-23 00:00:00 | 95033 |
| 105 | 王芳   || 1974-06-03 00:00:00 | 95031 |
| 106 | 陆君   || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋 || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱 || 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+

接下来我们学习一下怎么让查询结果进行排序,比如说:
七.以class降序查询students表中所有的记录
升序和降序使用到了order by…desc/asc ,如果是是降序使用secs升序使用asc
上面的查询:

select * from students order by class desc;

结果如下:

mysql> select * from students order by class desc;
+-----+--------+------+---------------------+-------+
| sno | sname  | ssex | sbirthday           | class |
+-----+--------+------+---------------------+-------+
| 101 | 曾华   || 1977-09-01 00:00:00 | 95033 |
| 103 | 王丽   || 1976-01-23 00:00:00 | 95033 |
| 104 | 李军   || 1975-02-10 00:00:00 | 95033 |
| 107 | 王尼玛 || 1976-02-20 00:00:00 | 95033 |
| 102 | 匡明   || 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳   || 1974-06-03 00:00:00 | 95031 |
| 106 | 陆君   || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋 || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱 || 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+

要说明一下,如果是不声明是升序或者降序的话,默认为升序,所以如果想要升序的话,asc其实可以简化为:

select * from students order by class

那如果我们想要一个联合的排序要怎么办呢?
比如:
八.在score表中以cno为升序degree为降序排列
其实很简单,用逗号把他们连接起来就好了,具体方法如下:

select * from score order by cno asc,degree desc;

查询结果:

mysql> select * from score order by cno asc,degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 105 | 3-105 |     88 |
| 109 | 3-105 |     76 |
| 103 | 3-245 |     86 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
| 103 | 6-166 |     85 |
| 109 | 6-166 |     81 |
| 105 | 6-166 |     79 |
+-----+-------+--------+

这个时候如果cno一样的时候会使用degree降序排列,特别需要注意的是,在这种多个排序条件的时候,升序是必须写asc的,是不可以省略的。

然后最后一个查询学习:统计个数
九.查询‘95031’班级人数
使用到了count关键字

select count(*) from students where class=‘95031’;

查询结果:

mysql> select count(*) from students where class='95031';
+----------+
| count(*) |
+----------+
|        5 |
+----------+
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值