Navicat中一些查找操作 笔记07

一、单表查询

1.查找所有(*)
slect * from st01   【注:以下操作代码中的st01是表格名字】 
2.where子语句中的一些运算符
-- =等于
-- >大于
-- >=大于等于
-- <小于
-- <=小于等于
-- !=不等于

select * from st01 where age=21
select * from st01 where age>18
select * from st01 where age>=20


3.逻辑运算符
-- and与
-- or 或
-- not非(主要用在in和is中使用)

select * from st01 where age>10 and age<21
select * from st01 where age<15 or age>20


4.范围查询
-- between...and... 介于范围之内
-- in 包含
-- not in 不包含

select * from st01 where id in(2,6,7)


-- is null 判空
-- is not null判断非空
5.模糊查询
-- like为模糊查询,需要配合占位符一起使用  _代表一位字符 %代表任意位字符

select * from st01 where name like "%t%" 


6.分页查询(限制查询)
-- limit a,b  a表示开始的索引值,b表示查询的个数

select * from st01 limit 4,10
-- 分页 一页放四条信息
-- 第一页
select *from stu limit 0,4
-- 第二页
select *from stu limit 4,4
-- 第三页
select *from stu limit 8,4
-- 一共能得到几页? 总页数/每页索引值
select count(*) from stu

-- 一页的大小pageSize 页码 page
select *from student limit (page-1)*pageSize,pageSize

【limit 子语句放到最后位置】

select * from st01 where sex="女" limit 0,2

-- 排序(where子语句之后,limit子语句之前)
-- order by 列名 desc降序 |asc升序

select * from st01 order by age desc


7.分组函数和聚合函数
-- sum() 取最小值
-- max() 取最大值
-- min() 取最小值
-- avg() 取平均值
-- count() 取得的记录数量
-- count(*)表示取得当前查询表的所有记录,count(字段名称)不会统计null

select count(age) from st01 

-- group by字段名称 分完组后查找用having

select min(age),class from st01 group by class having class=3

二、多表查询(一对一,一对多,多对多)

1. 一对一(e.g丈夫、妻子表)

①合并成一张表√(在一对一的连接处理中,最优)

②在其中一张表中加入外键,通过外键连接

③单独创建一张表,存关系×(不推荐,耗费更多空间和精力)

2. 一对多(e.g班级和学生,部门和员工)

①合并成一张表×(非常不适用,修改操作冗余)

②在多方加入外键√(在一对多的处理过程中最优)

③在单方加入外键×(操作冗余)

④单独创建一个表格存储关系(虽然无冗余,但是额外创建了资源,相比于②的处理方式,稍逊一筹)

3. 多对多×(e.g学生和课表)

①合成一张表×(信息冗余)

②加外键×(无论在哪一方加外键,都会造成冗余现象)

③创建新表存储关系√(在多对多关系中最优)

三、多表查询

-- 重命名 ...as...
 

select *from stu as S,class as C where S.cid=C.id

 特定输出查询:
 

select S.name,S.cid from stu as S,class as C where S.cid=C.id

四、连表查询
内连接(查询出所有的公共部分)

(将","换成join,将后面跟条件的where换成on--如果再需要条件,在最后面加上where子语句正常使用)

select *from stu join class on stu.cid=class.id where sex="女"

外连接:
--   左外连接(在公共部分的基础上增加左边区域表格内容)

select *from stu left join class on stu.cid=class.id

--      右外连接(在公共部分的基础上增右边区域表格内容)

select *from stu right join class on stu.cid=class.id

-- 多表内连-三个及以上的内连式写法(两种等效)

select *from stu join class join room on stu.cid=class.id and stu.rid=room.rid 
-- 两种写法等效
select *from stu join class on stu.cid=class.id join room on stu.rid=room.rid


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值