数据库的查询简单操作

1.使用distinct 关键字过滤重复的内容

//表示从students表中进行查询,去除sex,class相同的数据
SELECT DISTINCT sex, class from students;

2.使用where有条件的选择性进行查询

查询students 表中学号 studentNo 等于’001’ 的记录 
select * from students where studentNo = '001';

3.数据库sql语句中的比较运算符

=等于
< 小于
<= 小于等于
> 大于
>= 大于等于
!=<>不等于
--1:查询 students 表中 name(姓名)等于’小乔’学生的 age(年龄) 
select age from students where name = '小乔'; 
--2:查询 students 表中 30 岁以下的学生记录
 SELECT * from students where age < 30; 
 --3:查询 students 表中 30 岁和30岁以下的学生记录 
 SELECT * from students where age <= 30;
  -- 查询家乡不在'北京'的学生记录
   select * from students where hometown != '北京';
    select * from students where hometown <> '北京

4.逻辑运算符

**- and 表示并且的意思,指的是前后两个条件都满足

  • or 表示或者的意思,指的是前后的条件之间只需要满足一个条件就好了
  • not 表示非的意思,就是取反的意思,
    如果当前条件满足,加上not之后就表示不满足**
--1:查询 age 年龄小于 30,并且 sex 性别为’女’的同学记录 SELECT * from students where age < 30 and sex = '女'; 
--2:查询 sex 性别为’女’或者 class 班级为'1 班'的学生记录 SELECT * from students where sex = '女' or class = '1班'; 
--3:查询 hometown 老家非’天津’的学生记录 
SELECT * from students where not hometown = '天津'; 
--3:查询 hometown 老家’天津’的学生记录 
SELECT * from students where not hometown != '天津';

5.模糊查询

like实现模糊查询
%代表任意多个字符
_代表任意一个字符
字段名 like '字符%'

--1:查询 name 姓名中以’孙’开头的学生记录
 SELECT * from students where name like '孙%'; 
 --2:查询 name 姓名以’孙’开头,且名只有一个字的学生记录
  SELECT * from students where name like '孙_';

```javascript
```查询name姓名是以‘乔‘结尾的学生
SELECT * from students where name like '%乔'; 
-- 查询 name 姓名有’白’子的学生记录
 SELECT * from students where name like '%白%';

6.范围查找

in (值, 值, 值)
非连续范围查找
between 开始值 and 结束值
连续范围查找,包含开始值 包含 结束值

--1:查询 hometown 家乡是’北京’或’上海’或’广东’的学生记录 SELECT * from students where hometown = '北京' or hometown = '上海' or hometown = '广东';
 SELECT * from students where hometown in ('北京', '上海', '广东'); 
 --2:查询 age 年龄为 2530 的学生记录
  SELECT * from students where age >= 25 and age <= 30; 
  SELECT * from students where age between 25 and 30;

7.空判断

null 不是0,也不是’’,null在SQL中代表空什么也没有
null 不能用比较运算符的判断
is null ----是否为null
is not null —是否不为null

1:查询 card 身份证为 null 的学生记录 
SELECT * from students where card is null; -
-2:查询 card 身份证非 null 的学生记录 
- SELECT * from students where card is not null;

8.order by 排序

order by 字段名 [asc/desc]
asc代表从小到大,升序,asc可以省略
desc代表从大到小,不可以省略

1:查询所有学生记录,按 age 年龄从小到大排序 
 select * from students order by age asc; 
 select * from students order by age
例 2:查询所有学生记录,按 age 年龄从大到小排序 
 select * from students order by age desc; 

9.聚合函数

count求select返回的记录总数
count(字段名)

-- 查询学生总数(查询stuents表有多少记录) 
select count(*) from students; 
select count(name) from students; 
select count(DISTINCT class) from students; 
select count(DISTINCT sex) from students; 
-- 查询女同学数量 
SELECT count(name) from students where sex = '女'; 
SELECT count(*) from students where sex = '女'; 
SELECT count(sex) from students where sex = '女';

max查询最大值
max(字段名)
查询指定字段里的最大值

-- 查询students中的最大年龄 
SELECT max(age) from students; 
-- 查询students中的女生最大年龄 
SELECT max(age) from students where sex = '女'; 
-- 查询students中的'1班'最大年龄 
SELECT max(age) from students where class= '1班'

聚合函数不能用到where后面的条件里

min查询最小值
min(字段名)
查询指定字段的最小值

-- 查询students中的最小年龄 
SELECT min(age) from students; 
-- 查询students中的女生最小年龄 
SELECT min(age) from students where sex = '女'; 
-- 查询students中的'1班'最小年龄 
SELECT min(age) from students where class= '1班'

sum求和
sum(字段名)
指定字段的值求和

-- 查询students中的年龄总和 
SELECT sum(age) from students; 
-- 查询students中的女生年龄总和 
SELECT sum(age) from students where sex = '女'; 
-- 查询students中的'1班'年龄总和 
SELECT sum(age) from students where class = '1班';

avg求平均数
avg(字段名)
指定字段的平均值

 -- 查询students中的所有学生年龄总和 
SELECT sum(age) from students;
 -- 查询students中的女生年龄总和 
 SELECT sum(age) from students where sex = '女'; 
 -- 查询students中的'1班'年龄总和 
 SELECT sum(age) from students where class ='null'

avg的字段中如果有null,null不做为分母计算平均

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值