当前位置: 首页 > article >正文

后端-navicat查找语句(单表与多表)

 

表格字段设置如图

语句:

1.输出

1.输出name和age列 

SELECT name,age from student

1.2.全部输出 

select * from student 

2.where子语句

1.运算符:

=等于

>大于

>=大于等于

<小于

<=小于等于

!=    <>不等于

select * from student where age!=24

 

select * from student where age<24

2.逻辑运算符:

and 并且
or  或者
not 取非 主要用在is 或in

select * from student where age>=24 and sex="女" and name="admin"

select * from student where age>=24 or sex="男"

select * from student where age>=20 and age<=24

3.范围 

between ..and ..范围查询,在两个值之间

in 范围查询 包含

select * from student where id between 3 and 5

select * from student where id not in(1,4,5)

4.空 

is null 为null

select * from student where age is not null

模糊查询  like 为模糊查询  需要配合占位符一起使用  _代表一位字符  %代表任意位字符

3.分页查询

1.语句

limit a,b              ----a表示起始的索引值  b表示查询的个数

limit b offset a 

select * from student  limit 0,5

 

eg.

一页3个 pagesize

$1.第一页  page

select * from student  limit 0,3

$2. 第二页

select * from student  limit 3,3

$3. 第三页

select * from student  limit 6,3

 

$4.第四页

select * from student  limit 9,3

2.总结 :

select * from student  limit (page-1)*pagesize,pagesize

 3.位置:

limit 子语句放在最后位置

select * from student  where sex="男" limit 0,3

4.排序子语句

1.语句

order by 列名 desc降序排列 || asc升序

 

select * from student ORDER BY age desc

2.位置:

排序子语句的相对位置  

                在where子语句之后  在limit子语句之前

 

select * from student where sex="女" order by class_num desc limit 0,3

5.分组函数和聚合函数

1.聚合函数

-- sum()求和
-- min()取最小值
-- max() 取最大值
-- avg()取平均值
-- count()取得的记录数量  count(*)表示取得当前查询表的所有记录数  

                count(字段名称)不会统计为null的记录

select count(age) from student

 

select count(*) from student

 

2.分组函数

GROUP BY 字段名称          分完组之后做查找用having

select max(age),class_num from student GROUP BY class_num

select max(age),class_num from student GROUP BY class_num having class_num=2

2.多表查询

1.关系

1.1 一对一

 $ 1.合并成一张表 效果最好

$ 2. 在其中一张表加入外键

 &&or

$3. 单独创建一张表存关系

 1.2 一对多

学生对班级

$1. 合成一张表

缺点:有多少学生这些信息得存多少遍 数据冗余

$2. 在多方加入外键  效果最好

修改班级等方便, &&查看班级多少人,只需看cid有多少个

②在1方加入外键 数据冗余

$3. 单独创建一张表存关系

1.3 多对多

学生对课程

课程对学生

$1. 合并成一张表----->重复

$2. 加外键

在谁方谁冗余

①学生方

 ②课程方

$3. 单独创建一张表存关系

1.4 结论:

2.查询

$1. 建立班级表和学生表

 

$2. 添加外键

$3. 填入数据

$4. 查询

select * from 班级表,学生表 where 学生表.cid=班级表.id

-- 连表查询 join on
-- 内连接
-- 外连接   左外连接和右外连接

 把where的地方换成on

select * from 班级表 join 学生表 on 学生表.cid=班级表.id

#左连接 (公共的部分加上左边空的<独有的>)

select * from 班级表 left join 学生表 on 学生表.cid=班级表.id

 

#右连接(公共的部分加上右边空的<独有的>)

select * from 班级表 right join 学生表 on 学生表.cid=班级表.id


http://www.kler.cn/news/315720.html

相关文章:

  • atcoder abc372 启发式合并, dp
  • 感知算法引入时序模型的优势
  • Unity UGUI的核心渲染组件
  • FFmpeg中结构释放小函数
  • Python在数据科学与机器学习中的应用
  • C语言 | Leetcode C语言题解之第429题N叉树的层序遍历
  • Nginx简介;Nginx安装
  • Chainlit集成LlamaIndex实现知识库高级检索(自动合并检索)
  • VUE3学习---【一】【从零开始的VUE学习】
  • Java面试篇基础部分-Synchronized关键字详解
  • python爬虫中json和xml字符串的xPath和jsonpath过滤语法区别对比
  • 零工市场小程序:推动零工市场建设
  • 【Kubernetes】常见面试题汇总(三十)
  • 【二等奖论文】2024年华为杯研赛D题成品论文(后续会更新)
  • rust GTK4 窗口创建与 wayland Subsurface (vulkan 渲染窗口初始化 (Linux) 上篇)
  • Docker实践——天池篇
  • 极度精简 Winows11 系统镜像!Tiny11 2311下载 - 支持苹果 M 芯片 Mac 安装 (ARM 精简版)!
  • get_property --Cmakelist之中
  • 关闭小广告【JavaScript】
  • 【线程】线程的同步
  • PHP转Go很丝滑开发框架设计思路-把php优秀设计借鉴到Go框架设计里面-保留php开发习惯又能提供高软件性能
  • OpenCV特征检测(8)检测图像中圆形的函数HoughCircles()的使用
  • 利用JAVA写一张纸折叠珠穆拉玛峰高度
  • 算法打卡:第十一章 图论part04
  • 情指行一体化平台建设方案和必要性-———未来之窗行业应用跨平台架构
  • 0基础学习PyTorch——最小Demo
  • AI教你学Python 第17天 :小项目联系人管理系统
  • 小程序-模板与配置
  • 乱弹篇(53)丹桂未飘香
  • Excel常用函数大全