数据库学习(四)—SQL数据查询01(简单方法&条件查询)

目录

3.1 简单方法

3.1.1 查询指定字段,取别名

3.1.2 消除重复行

 3.2 条件

3.2.1 比较运算符:

 3.2.2 逻辑运算符

 3.2.3 模糊查询

 3.2.4 范围查询

 3.2.5  空判断


创建数据表,后续的数据表查询以此为准

 创建数据表

drop table if exists students;
create table students (
    studentNo varchar(10) primary key,
    nane varchar(10),
    sex varchar(1),
    hometown varchar(20),
    age tinyint(4),
    class varchar(10),
    card varchar(20)
)

准备数据


insert into students values
('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
('002', '诸易亮', '男', '上海', '18', '2班', '340322199002242354'),
('003','张飞',  '男',  '南京','24','3班','340322199003247654'),
('004','白起',  '男',  '安徽','22','4班','340322199005247654'),
('005','大乔' , '女',   '天津','19','3班',  '340322199004247654'),
('006','孙测香', '女', '河北', '18', '1班', '340322199006247654'),
( '007','百里玄闟','男','山西','20', '2班', '340322199087247654'),
('008','小乔','女', '河南','15','3班',null),
('009','百里守约','男','湖南','21','1班',''),
('010','妲己','女','广东', '26', '2班','340322199607247654'),
('011','李白','男', '北京', '30', '4班','340322199005267754'),
('012','孙膑','男','新疆','26','3班','340322199000297655')

 ·查询所有字段

select * from 表名
例:
-- * 代表所有字段,所有列的意思
select * from  students

3.1 简单方法

3.1.1 查询指定字段,取别名

查询指定字段,取别名
·在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中

 select  列1,列2 :只能显示哪几列,不能限制哪几行,哪行不显示

select 列1,列2, ... from 表名

-- 表名.字段名
select students.name, students.age from students

_____________________________________________________

-—可以通设as给表起别名
select s.name,s.age from students as s

_____________________________________________________

--如果是单表查淘司以省略表名
select name,age from students

_____________________________________________________

一使用as给字段起别名
select studentNo as 学号,name as 名字,Sex as 性别  from students


🧐例1:显示学生的名字和家乡信息

select nane,hometown from students

🧐例2:利用给字段起别名的方式显示学生的名字和家乡信息

-- 给字段起别名
select nane as 姓名, hometown as 家乡 from students

🧐例3:利用给表起别名的方式显示学生的名字和家乡信息

-- 给表起别名,可以把表的别名添加到字段中
-- 这种方式查询一张表是不需要的,若是多张表,这时就需要进行判断
select stu.nane as 姓名, stu.hometown as 家乡 from students as stu

 ❗注意:涉及多表查询的时候,通常涉及别名

3.1.2 消除重复行

 在select后面列前使用distinct可以消除重复的行,去除重复的数据

select distinct 列l, ... from表名:
例:
select distinct sex from students;

🧐 例1:个字段去除重复数据 

🧐例2: 多个字段去除重复数据

 

 3.2 条件

条件查询:到底要哪几行数据。
使用where子句对表中的数据筛选,符号条件的数据会出现在结聚集中

语法如下:
先写哪个表,然后再写需要的条件,最后再看到底要显示哪几列。

select后面仅显示,并非过滤作用,where后面才是过滤

select 字段1,字段2 ...  from 表名 where 条件; 

例:
select * from students where id=1;


 🔶where后面支持多种运算符,进行条件的处理

  • 比较运算
  • 逻辑运算
  • 模糊查询
  • 范围查询
  • 空判断


3.2.1 比较运算符:

  • 等于: =
  • 大于: >
  • 大于等于: >=
  • 小于:<
  • 小于等于:<=
  • 不等于:!=或<>

🧐例子:

例1∶查询小乔的年龄

select age from students where name='小乔'


例2:查询20岁以下的学生

select * from students where age<20

 


例3:查询家乡不在北京的学生

select * from students where hometown1!='北京'

🧐练习;

  1. 查询学号是"007"的学生的身份证号
    select card from students where studentNo = '007'

  2. 查询"1班“以外的学生信息
    select * from students where class !='1班'

  3. 查询年龄大于20的学生的姓名和性别
    select nane, sex from students where age > 20

 3.2.2 逻辑运算符

  •  and  且
  • or  或
  • not  非

🧐例子:

例1:查询年龄小于20的女同学

select * from students where age < 20 and sex = '女'


例2:查询女学生或'1班'的学生

select * from students where sex="女”or class='1班'


例3:查淘非天津的学生

select* from students where not hometown='天津'

 


🧐练习;

  1. 查询河南或河北的学生
    select * from students where  hometown = '河南' or hometown = '河北'
  2. 查询*1班'的'上海"的学生
    select * from students where  class = '1班' and hometown = '上海'
  3. 查询非20岁的学生
    select * from students where  not age  = 20

 3.2.3 模糊查询

模糊查询解释:

        如搜索测试关键词, 找到 xxx测试 ,xxx测xxx试、 测试xxx等情况,

        模糊查询就是做匹配,输入一个关键字,只要任何一个文本里面包含这个关键字都会给其显示

语法:

  •  like
  • %表示任意多个任意字符
  • _表示一个任意字符

🧐例子:

例1:查询姓孙的学生

sclect * from students where name like'孙考'
select * from students where  nane like '孙%'

 

例2:查询姓孙且名字是一个字的学生

select * from students where name like  '孙_'

--孙后2个字
select * from students where  nane like '孙__'

例3:查询叫乔的学生
 

select * from students where name like '%乔'


 例4:查询名字含白的学生

select * from students where  nane like '%白%'

🧐练习;

  1. 查询姓名为两个字的学生
    select * from students where  nane like '__'
  2. 查询姓百且年龄大于20的学生
    select * from students where  nane like '百%' and age > 20
  3. 查询学号以1结尾的学生
    select * from students where  studentNo like '%1' 

 3.2.4 范围查询

范围查询:在查找字段时候,有可能存在多个值

  • in表示在一个非连续的范围
  • between ..A. and  .B..表示在一个连续的范围内(注意:A < B)

🧐例子:

例1∶查询家乡是北京或上海或广东的学生

-- 方法一:
-- select * from students where hometown = '北京' or hometown = '上海' or hometown = '广东'
-- 方法二:
select * from students where hometown in( '北京',  '上海', '广东')



例2:查询年龄为18至20的学生

-- 方法一:
-- select * from students where age  = 18 or age  = 19 or age  = 20

-- 方法二:
-- select * from students where age in (18, 19, 20)

-- 方法三:
-- select * from students where age between 18 and 20


-- 方法四:
select * from students where age >= 18 and age <=20

 


🧐练习;

  1. 查询年龄在18或19或22的女生
    select * from students where age in(18, 19, 22) and sex = '女'
  2. 查询年龄在20到25以外的学生
-- 方法一:
-- select * from students where age not  between 20 and 25

-- 方法二:
select * from students where age < 20 or age >25

 3.2.5  空判断

  • 注意: null 与 ‘’ (空字符串) 是不同

 如:考试交白卷(空字符串)与没交(null)的区别

怎么创建:

一插入时直接设置null

insert into students values('013', 'jane', '女', '北京', 20, '1班', ''),
('014', 'amy', '女', '北京', 23, '1班', null)

 怎么插入数据会变成null。无给字段插入东西

insert into students(studentNo,nane) values('015', 'tom')

  • 判空 is null
  • 判非空 is not null

🧐例子:

例1﹔查询没有填写身份证的学生

-- select * from students where card = null 无法使用
select * from students where card is null



例2∶查询填写了身份证的学生

-- 方法一:
-- select * from students where not card  is null
-- 方法二:
select * from students where  card  is not null

 

例1:查询没有填写身份证的学生
select* from students where card is null

例2:查询填写了身份证的学生
select * from students where card is not null

' '代表插入的致据为空字符串
insert into students values("013'。'凯'。男",'北京",20,"1班","')

null代表插入的值为空
insert into students values('014','凯2',‘男',"北京', 20, '1班' ,null)

插入数据时,如果一些字段没指定效据。默认为null
insert into students(studentNo,name) values("015',"凯3')
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值