node知识点总结(十)

181 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第27天,点击查看活动详情

前言

大家好呀,我是L同学。在上篇文章node知识点总结(九)中,我们学习了node相关知识点,包括MySQL介绍、安装操作MySQL的图形化工具Navicat、创建表、导入导出数据表、sql语句等相关知识点。今天,在这篇文章中,我们来学sql语句,包括带where子句的查询、模糊查询、统计查询、查询结果排序、限制查询结果等相关知识点。

sql语句

带where子句的查询

select field1, field2... from 表名 查询表中的所有数据。

where 可以使用条件来筛选查询出的结果。

查询id小于10的英雄。

select * from heroes where id<10

查询id小于20的女英雄。

select * from heroes where id<20 and sex='女'

查询年龄大于等于20小于等于30的英雄。

select * from heroes where age>=20 and age<=30
select * from heroes where age between 20 and 30

模糊查询

通配符:

  • %: 代表任意长度(包括0)的任意字符
  • _: 代表1位长度的任意字符

like: 在执行模糊查询时,必须使用like来作为匹配条件

查询名字中带有 “斯” 的英雄。

select * from heroes where name like '%斯%'

查询名字的最后一个字是 “斯” 的英雄。

select * from heroes where name like '%斯'

查询名字中带有 “斯” ,并且要求 “斯”前面只能有一个字的英雄。

select * from heroes where name like '_斯%'

统计查询

  • max 查询最大值 select max(age) from heroes
  • Min 查询最小值 select min(age) from heroes
  • avg 查询平均值 select avg(age) from heroes
  • sum 查询总和(查询所有英雄的年龄之和) select sum(age) from heroes
  • count 查询总记录数(查询共计有多少个英雄) select count(*) cc from heroes

查询结果排序

order by 可以对查询结果按某个字段进行升序或者降序排列

  • 升序 asc (默认值)
  • 降序 desc

可进行排序的字段通常是 整型 英文字符串型 日期型 (中文字符串也行,但一般不用)。需要注意的是,如果SQL语句中,有where和order by,where一定要放到order by之前。

查询所有的英雄,按年龄升序排序。

select * from heroes order by age asc
select * from heroes order by age

查询所有的英雄,按年龄降序排序。

select * from heroes order by age desc

查询所有的英雄,先按年龄降序排序;如果年龄相同的,再按id降序排序。

select * from heroes order by age desc, id desc

查询年龄大于50岁的英雄,并按年龄降序排序。

select * from heroes where age>50 order by age desc

限制查询结果

limit 用来限制查询结果的起始点和长度。

  • 格式: limit start, length

    • start: 起始点。 查询结果的索引,从0开始。 0代表第一条数据。如果省略start,则默认表示从0
    • length: 长度 需要注意的是,where、order by、limit如果一起使用,是有顺序的,where在最前面、其次是order by、limit要放到最后。另外三者之间没有and之类的。

查询所有英雄中前5个英雄。

select * from heroes limit 起始位置, 长度
select * from heroes limit 0, 5
select * from heroes limit 5

查询所有英雄中,第6到10个英雄。

select * from heroes limit 5, 5

查询年龄最大的3个英雄。

select * from heroes order by age desc limit 3

查询年龄最大的3个女英雄。

select * from heroes where sex='女' order by age desc limit 3

连接查询

当一张表中的数据不能满足我们查询数据的需求时,我们要考虑多个表进行连接查询。MySQL中的连接查询,能够连接查询的两张或多张表,必须有关系才行。

语法:

select 字段 from 表1 join 表2 on 两表的关系 [where ... order by .... limit ...]

select 字段 from 表1 join 表2 on 两表的关系 join 表3 on 表的关系 .... [where ... order by .... limit ...]

select * from student join teacher on student.teacher_id=teacher.Id

给表名定义别名,简化SQL。

select * from student s join teacher t on s.teacher_id=t.Id 

指定字段查询。

select s.id, s.name, s.age, s.sex, t.name teacher_name from student s join teacher t on s.teacher_id=t.Id 

总结

  • 控制查询的列数

    • select 字段, 字段, ... from heroes ---- 选择查询指定的列
    • select * from heroes ---- 查询所有的列
  • 控制查询的行数

    • where 条件

      • where id>3
      • where id>3 and age<30
      • where id<3 or sex='男'
      • where name like '%斯%'
    • order by 字段 排序规则

      • order by age asc
      • order by age desc
    • limit 起始位置, 长度

      • limit 0, 3 相当于 limit 3
      • limit 3, 3
  • 连接查询

    • select 字段 from 表1 join 表2 on 两表的关系 join 表3 on 表的关系 .... [where ... order by .... limit ...]

select 字段 from 表名 [where 条件] [order by 排序字段 排序规则] [limit 起始位置,长度]

select count(*) as total from 表名 -- 查询总记录数