Sql常见实用题

一:创建数据表:

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)
)

1.1准备数据

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 * frome students

二:简单方法

2.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

2.2消除重复行

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

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

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

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

三:条件

使用where子句对表中的数据筛选,符号条件的数据会出现在结聚集中

语法如下:

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

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

select 字段1,字段2 ...  from 表名 where 条件; 
 
例:
select * from students where id=1;

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

  • 比较运算

  • 逻辑运算

  • 模糊查询

  • 范围查询

  • 空判断

3.1比较运算符

  • 等于: =

  • 大于: >

  • 大于等于: >=

  • 小于:<

  • 小于等于:<=

  • 不等于:!=或<>

例子1:查询小巧的年龄
select age from students where name = "小乔"
例子2:查询20岁以下的学生
select * from students where age<20
例子3:查询家乡不在北京的学生
select * frome students where hometown !="北京"

练习:

1.查询学号是"007"的学生的身份证号
select card from students where studentno = "007"
2.查询年龄大雨20的学生的名字和性别
select name,sex from students where age>20
2.查询“1班”意外的学生信息
select * from students where !="1班"

3.2逻辑运算符

  • and 且

  • or 或

  • not 非

例子

1查询年龄小于20的女同学
select * from students where age<20 and sex="女"
2查询女生或者1班的同学
selct * from students where sex="女" or class="1班"
3查询非天津的学生
select * from students where not hometown ="天津"

练习

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

3.3模糊查询

模糊查询解释:

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

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

语法:

  • like

  • %表示任意多个任意字符

  • _表示一个任意字符

例子

1.查询姓孙的学生
select * from students where name like "孙%"
2.查询姓孙名字且一个字的学生2
select * from students where name like "孙_"
2.1孙后面2个字
select * from students where name like "孙__"
3。查询叫乔的学生
select * from students where name like "%乔"
4.查询名字含白的学生
select * from students where name like "%白%"

😊练习

查询名字为两个字的学生
select * from students where name like "__"
2查询姓百年龄切大于20的学生
select * from students where name like"百%" and age>20
3。查询学号以1结尾的学生
select *from students where studentno like "%1" 

3.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)

2.查询年龄在20到25以外的学生
select * from students where age <20 or age< 25

3.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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SQL Queries for Mere Mortals》是一本以通俗易懂的方式介绍SQL查询语言的书籍。这本书适合于初学者和非专业人士,通过简洁明了的解释和示例,帮助读者理解和运用SQL查询语言。 该书分为三个部分,分别是"入门篇"、"进阶篇"和"高级篇"。在入门篇中,作者从基本的数据库概念开始,介绍了如何创建和管理数据库以及表格。然后,他详细讲解了SQL查询语句的基本语法和常见查询操作,如SELECT、FROM、WHERE、ORDER BY等。通过实际的例子,读者可以逐步掌握SQL查询的基本技巧。 在进阶篇中,作者介绍了更复杂的查询操作和高级技巧。例如,使用连接(JOIN)进行多表查询、使用聚合函数进行统计和汇总、使用子查询进行嵌套查询等。这些内容对于想要深入学习SQL查询语言的读者来说非常有用。 最后,在高级篇中,作者讨论了一些高级的话,如窗口函数、递归查询和存储过程。这些知识对于处理复杂的分析和数据处理任务非常重要。 《SQL Queries for Mere Mortals》提供了许多实用的技巧和经验,帮助读者更好地理解和应用SQL查询语言。无论是初学者还是非专业人士,都可以通过阅读这本书来快速入门并掌握SQL查询技能。不仅如此,该书还提供了大量的练习和答案,读者可以通过实践来巩固所学知识。总体而言,这本书是一本很好的入门指南,能够帮助读者轻松地学习和运用SQL查询语言。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SectIon.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值