MySQL单表查询


单表查询
查询列
查询表中指定列的数据
select 【列名1】,【列名2】
from 【表名称】;

查询所有列的数据
select *
from 【表名称】;
查询列经过计算后的值
select 【关于某列的目标表达式】
from 【表名称】;
mysql> select sno
from student;
+-----------+
| sno |
+-----------+
| 201215121 |
| 201215122 |
| 201215123 |
| 201215125 |
+-----------+
4 rows in set

mysql> select sno+2
from student;
+-----------+
| sno+2 |
+-----------+
| 201215123 |
| 201215124 |
| 201215125 |
| 201215127 |
+-----------+
4 rows in set
mysql> select sno+2 ,sno +4

from student;
+-----------+-----------+
| sno+2 | sno +4 |
+-----------+-----------+
| 201215123 | 201215125 |
| 201215124 | 201215126 |
| 201215125 | 201215127 |
| 201215127 | 201215129 |
+-----------+-----------+
4 rows in set

为查询出来的数据添加解释
select "列注释" 自己添加的注释,【源数据列名】 【显示的列名】
from student;
mysql> select "sno的值" 自己添加的注释,sno 学号
from student;
+----------------+-----------+
| 自己添加的注释 | 学号 |
+----------------+-----------+
| sno的值 | 201215121 |
| sno的值 | 201215122 |
| sno的值 | 201215123 |
| sno的值 | 201215125 |
+----------------+-----------+


在空【源数据列名】 【显示的列名】中间可以使用 as关键字 也可以仅仅留下空白

查询元组
自动去重查询
select distinct 【列名称】
from 【表名称】;
mysql> select sdept from student;
+-------+
| sdept |
+-------+
| CS |
| CS |
| MA |
| IS |
+-------+
4 rows in set

mysql> select distinct sdept from student;
+-------+
| sdept |
+-------+
| CS |
| MA |
| IS |
+-------+

若没有distinct 实际上
select 【列名称】
from 【表名称】;

等价于

select all【列名称】
from 【表名称】;

条件查询
使用where子句实现
比较
等于
=
不等于
!= ,<>
大于等于,小于等于(等于符号永远在右边)
>= ,<=
不大于,不小于(感叹号永远在左边)
!> ,!<
使用 not+上述运算符实现非逻辑
 
确定范围
在。。。之间
between and
not between and
集合
在。。。集合中
in
not in
字符匹配
like
not like
 
逻辑运算
and not or
 
比较大小
mysql> select sage, sname from student where sage<20;
+------+-------+
| sage | sname |
+------+-------+
| 19 | 刘晨 |
| 18 | 王敏 |
| 19 | 张立 |
+------+-------+
范围内查找
mysql> select sage, sname
from student
where sage between 18 and 19;
+------+-------+
| sage | sname |
+------+-------+
| 19 | 刘晨 |
| 18 | 王敏 |
| 19 | 张立 |
+------+-------+
集合内查找
mysql> select* from student;

+-----------+-------+------+------+-------+
| Sno | Sdept | Sage | Ssex | Sname |
+-----------+-------+------+------+-------+
| 201215121 | CS | 20 | 男 | 李勇 |
| 201215122 | CS | 19 | 女 | 刘晨 |
| 201215123 | MA | 18 | 女 | 王敏 |
| 201215125 | IS | 19 | 男 | 张立 |
+-----------+-------+------+------+-------+
4 rows in set

mysql> select sname from student where sdept in ('MA','is');
+-------+
| sname |
+-------+
| 王敏 |
| 张立 |
+-------+

mysql> select sname from student where sdept not in ('MA','is');
+-------+
| sname |
+-------+
| 李勇 |
| 刘晨 |
+-------+
字符匹配查找
like '匹配串'
not like ‘匹配串’

% :表示任意长度的字符串
_ :占位一个字符位,表示任意一个字符

查找年龄中含有字符 “1” 的学生的信息
mysql> select sage,sname from student where sage like '%1%';
+------+-------+
| sage | sname |
+------+-------+
| 19 | 刘晨 |
| 18 | 王敏 |
| 19 | 张立 |
+------+-------+
3 rows in set
要查询含有下划线 “_” 的匹配串怎么办?
使用换码字符 “ \_ ” 来实现。

多重条件查询
使用逻辑运算符进行语句连接
逻辑运算
and not or


mysql> select sage,sname from student where sage like '%1%' and sage <19;
+------+-------+
| sage | sname |
+------+-------+
| 18 | 王敏 |
+------+-------+
order by子句
使用该子句,对查询结果按照,一个或多个属性列的升序(ASC)或降序排序(DESC),默认升序排列、
查询结果按年龄排序
mysql> select sage,sdept,sname from student order by sage ASC;
+------+-------+-------+
| sage | sdept | sname |
+------+-------+-------+
| 18 | MA | 王敏 |
| 19 | CS | 刘晨 |
| 19 | IS | 张立 |
| 20 | CS | 李勇 |
+------+-------+-------+
查询结果先按学系 sdept 排序,再在同一个系中,按照年龄排序
mysql> select sage,sdept,sname from student order by sdept ASC,sage ASC;
+------+-------+-------+
| sage | sdept | sname |
+------+-------+-------+
| 19 | CS | 刘晨 |
| 20 | CS | 李勇 |
| 19 | IS | 张立 |
| 18 | MA | 王敏 |
+------+-------+-------+
聚集函数(是对在没有聚集函数时候获得的查询结果,的操作)
count (*);
统计元组个数
count(【distinct | all】【列名】)
计算一列中值的个数
sum(【distinct | all】【列名】)
计算一列值的总和(必须是数值型)
avg(【distinct | all】【列名】)
计算一列值的平均(必须是数值)
max(【distinct | all】【列名】)
求一列值的最大值
min(【distinct | all】【列名】)
一列值的最小值

计算student表中的数据条数,(元组个数)
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set
先在student表中,找出年龄小于20岁的学生,然后计算有多少个这样的学生
mysql> select count(sage) from student where sage < 20;
+-------------+
| count(sage) |
+-------------+
| 3 |
+-------------+
1 row in set
先在student表中,找出年龄小于20岁的学生,然后计算他们的平均年龄
mysql> select AVG(sage) from student where sage < 20;
+-----------+
| AVG(sage) |
+-----------+
| 18.6667 |
+-----------+
tips :
除了count(*)外,其余的聚集函数 都跳过会空值
mysql> select * from course;
+-----+------------+------+---------+
| Cno | Cname | Cpno | Ccredit |
+-----+------------+------+---------+
| 1 | 数据库 | 5 | 4 |
| 2 | 数学 | NULL | 2 |
| 3 | 信息系统 | 1 | 4 |
| 4 | 操作系统 | 6 | 3 |
| 5 | 数据结构 | 7 | 4 |
| 6 | 数据处理 | NULL | 2 |
| 7 | PASCAL语言 | 6 | 4 |
+-----+------------+------+---------+
7 rows in set

mysql> select count(*) from course;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set

mysql> select count(cpno) from course;
+-------------+
| count(cpno) |
+-------------+
| 5 | (此处仅仅查询到了五条记录)
+-------------+
group by 子句
group by 子句将查询结果按照某一列或某几列的值进行分组
分组后聚集函数将作用于每一个小组
首先,系统调出选课名单,
然后对选课名单按照选课类别进行分类
分类后使用聚集函数 count 作用于每一个小组的结果,算出选课的人数

mysql> select cno, count(sno) from sc group by cno;
+-----+------------+
| cno | count(sno) |
+-----+------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
+-----+------------+

having子句
having子句 的后边写分组需要满足条件,having 和where 的不同在于,
where作用于基本表,或者基本视图
having作用于组,筛选出满足having后边条件的组
首先,系统调出选课名单,
然后对选课名单按照选课类别进行分类
分类后使用聚集函数 count 作用于每一个小组的结果,算出各组选课的人数
然后使用having语句筛选满足条件()的那些组进行输出

mysql> select cno, count(cno) from sc group by cno having count(*)<2;
+-----+------------+
| cno | count(cno) |
+-----+------------+
| 1 | 1 |
+-----+------------+


猜你喜欢

转载自blog.csdn.net/qq_37755810/article/details/80299765