MySQL数据查询

1.select查询语句

  • 语法:select 字段名1,字段名2 from 表

    -- 查询所有学生
    select * from `student`
    
    -- 查询指定字段
    select `name`,`pwd` from `student`
    
  • 别名,as可以给字段起名,也可以给表取名

    select `name` as 姓名,`pwd` as 密码 from `student` as s
    
  • 函数 concat(a,b),拼接字符串在查询结果里

    select concat('姓名:',name) as 新名字 from `student`
    
  • 去重:去除select查询出来的结果中重复的数据

    select distinct 字段名 from 表名
    
  • 查看系统版本

    select version()
    
  • 用来计算

    select 100*3-1 as 计算结果
    
  • 查询自增的步长

    select @@auto_increment_increment
    
  • 学生年级全部+1

    select `name`,`gradeid`+1 as 新年级 from `student`
    

2.where条件子句

  • 作用:检索数据中符合条件的值

  • 逻辑运算符(尽量使用英文,少用符号)

    运算符语法描述
    and或&&a and b 或 a&&b逻辑与,两个为真,则结果为真
    or或||a or b 或 a||b逻辑或,其中一个为真,则结果为真
    not或!not a 或 !a逻辑非,真为假,假为真
    -- 查询成绩在95到100之间的学生
    select `studentName`,`score` from `result` where `score` >= 95 and `score` <= 100;
    select `studentName`,`score` from `result` where `score` >= 95 && `score` <= 100;
    select `studentName`,`score` from `result` where `score` between 95 and 100;
    
    -- 查询除小红外所有人的成绩
    select `studentName`,`score` from `result` where `studentName` != '小红';
    select `studentName`,`score` from `result` where not `studentName` = '小红';
    
  • 模糊查询:比较运算符

    运算符语法描述
    is nulla is null如果操作符为null,结果为真
    is not nulla is not null如果操作符为not null,结果为真
    between…and…a between b and c若a在b和c之间,则结果为真
    likea like bSQL匹配,若a匹配到b,则结果为真
    ina in(a1,a2,a3…)假设a在a1或者a2…其中的某一个值中,结果为真
    -- 查询姓刘的同学
    -- like结合 %(代表0到任意个字符) _(一个字符)
    select * from `student` where `name` like '刘%';
    -- 查询姓刘的后面有两个字的同学
    select * from `student` where `name` like '刘__';
    
    --查询id为1001、1002、1003、1004的学生
    select * from `student` where `id` in (1001,1002,1003,1004);
    
    -- 查询地址为空的学生
    select * from `student` where `address` = '' or `address` is null;
    
    -- 查询出生日期不为空的学生
    select * from `student` where `birthday` != '' or `birthday` is not null;
    

3.联表查询join

7种SQL联表查询

例1:查询出学生的学号、姓名、科目id和成绩

前提:result表中有学生学号、成绩和科目id,student表中有学生学号和姓名

方法一:inner join

-- as可以省略
select s.`studentNo`,`studentName`,`subjectNo`,`source` 
from `student` as s  
inner join `result` as r
on s.`studentNo` = r.`studentNo`

方法二:right join

select s.`studentNo`,`studentName`,`subjectNo`,`source` 
from `student` as s  
right join `result` as r
on s.`studentNo` = r.`studentNo`

left join:查出未参加考试的同学

select s.`studentNo`,`studentName`,`subjectNo`,`source` 
from `student` as s  
left join `result` as r
on s.`studentNo` = r.`studentNo`
where `source` is null
  • join 连接的表 on 判断的条件 (连接查询)
  • where (等值查询)
  • 对比
操作描述
inner join如果表中至少有一个匹配,就可以返回匹配的值
right join会从右表中返回所有的值,即使左表中没有匹配
left join会从左表中返回所有的值,即使右表中没有匹配

例2:查询出学生的学号、姓名、科目名和成绩

前提:result表中有学生学号、成绩和科目id,student表中有学生学号和姓名,subject表中有科目id和科目名

select s.`studentNo`,`studentName`,`subjectName`,`score`
from `student` s
right join `result` r
on r.`studentNo` = s.`studentNo`
inner join `subject` sub
on r.`subjectNo` = sub.`subjectNo`

自连接

category表:pid为父类id,categoryid为自身id

categoryidpidcategoryName
31软件开发
51美术设计
43数据库
82办公信息
21信息技术
63web开发
75ps技术

自连接就是自己的表和自己的表连接,核心:一张表拆为两张表即可

父类表:pid为1就是顶级父类

categoryidcategoryName
2信息技术
3软件开发
5美术设计

子类表:子类pid=父类categoryid,即该父类包含该子类

pidcategoryidcategoryName
34数据库
28办公信息
36web开发
57ps技术

操作:查询父类对应的子类关系

父类子类
信息技术办公信息
软件开发数据库
软件开发web开发
美术设计ps技术
select a.`categoryName` as '父类',b.`categoryName` as '子类'
from `category` as a, `category` as b
where a.`categoryid` = b.`pid`

4.分页和排序

排序:升序asc ,降序desc

  • 语法:select 字段名1,字段名2 from 表 order by 排序条件 asc|desc

    select * from `result` order by `score` desc
    

分页

  • 语法:select 字段名1,字段名2 from 表 limit 起始值(从零开始),页面大小

    select * from `result` limit 0,5
    
  • 程序中的分页代码

    • (n-1)*pageSize:起始值
    • pageSize:页面大小
    • n:当前页
    • Math.ceil(数据总数/页面大小)=总页数:向上取整(或者三目运算判断)

5.子查询

  • 本质:在where语句中嵌套一个子查询语句

    例:查询数据库结构-1所有考试的结果(学号、科目id、成绩),降序排列

    方式一:使用联表查询

    select `studentNo`,r.`subjectNo`,`score`
    from `result` r
    inner join `subject` sub
    on r.`subjectNo` = sub.`subjectNo`
    where `subjectName` = '数据库结构-1'
    order by `score` desc
    

    方式二:使用子查询(由里及外)

    select `studentNo`,`subjectNo`,`score`
    from `result`
    where `subjectNo` = (
        select `subjectNo` from `subject`
    	where `subjectName` = '数据库结构-1'
    ) order by 'score' desc
    

6.分组和过滤

  • group by:指定结果按照哪几个字段来分组

  • having:过滤分组的记录必须满足的次要条件

    例:查询不同课程的平均分、最高分、最低分,且平均分要大于等于80

    select `subjectName`,avg(`score`),max(`score`),min(`score`)
    from `result` r
    inner join `subject` sub
    on r.`subjectNo` = sub.`subjectNo`
    group by r.`subjectNo`
    having avg(`score`) >= 80
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Remote_Li

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

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

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

打赏作者

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

抵扣说明:

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

余额充值