首页 > 数据库 >数据库基础3:as关键字、distinct去重关键字、where条件查询、排序、分页查询

数据库基础3:as关键字、distinct去重关键字、where条件查询、排序、分页查询

时间:2023-02-07 16:36:31浏览次数:41  
标签:distinct 查询 关键字 students -- where id select

1. as关键字

在使用SQL语句显示结果的时候,往往在屏幕显示的字段名并不具备良好的可读性,此时可以使用 as 给字段起一个别名。

--使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;

-- 表名.字段名
select students.id,students.name,students.gender from students;

-- 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;

2. distinct关键字

distinct可以去除重复数据行。

-- 看到了很多重复数据 想要对其中重复数据行进行去重操作可以使用 distinct
select distinct name, gender from students;

3. where条件查询

使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。

where 语句支持的运算符:

  1. 比较运算符
  2. 逻辑运算符
  3. 模糊查询
  4. 范围查询
  5. 空判断

比较运算符

-- 查询id等于1的学生
select * from students where id = 1;

--查询id大于3的学生
select * from students where id > 3;

--查询id不大于4的学生
select * from students where id <= 4;

--查询姓名不是"黄蓉"的学生
select * from students where name != '黄蓉';
select * from students where name <> '黄蓉';

--查询没被删除的学生
select * from students where is_delete=0;

逻辑运算符

--查询id大于3 且 是女同学
select * from students where id > 3 and gender=0;
--查询id小于4 或 没被删除的同学
select * from students where id < 4 or is_delete=0;
-- 查询年龄不在10~15岁之间的同学
select * from students where not (age >= 10 and age <= 15);

模糊查询

  1. like是模糊查询关键字
  2. %表示任意多个任意字符
  3. _表示一个任意字符
--查询姓黄的学生
select * from students where name like '黄%';
--查询姓黄并且"名"是一个字的学生
select * from students where name like '黄_';
--查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';

范围查询

  1. between … and … 表示在一个连续的范围内查询
  2. in 表示在一个非连续的范围内查询
--查询id在3~8范围的学生
select * from students where id between 3 and 8;
--查询id不在3~8范围,且性别为男性的学生
select * from students where (not id between 3 and 8) and gender='男';

空判断查询

  1. 判断为空使用: is null
  2. 判断非空使用: is not null

注 意:

  1. 不能使用 where height = null 判断为空
  2. 不能使用 where height != null 判断非空
  3. null 不等于 ‘’ 空字符串
--查询没有填写身高的学生
select * from students where height is null;

4. 排序

语法:

select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]

语法说明:

  1. 先按照列1进行排序,如果列1的值相同时,则按照 列2 排序,以此类推
  2. asc从小到大排列,即升序
  3. desc从大到小排序,即降序
  4. 默认按照列值从小到大排列(即asc关键字)
-- 查询未删除男生信息,按学号从大往小降序排列
select * from students where gender=1 and is_delete=0 order by id desc;
-- 显示所有学生信息,先按年龄从大往小排序,年龄相同再按照身高排序
select * from students  order by age desc,height desc;

5. 分页查询

语法:

select * from 表名 limit start,count

参数说明:

  1. limit是分页查询关键字
  2. start表示开始行索引,默认是0
  3. count表示查询条数
select * from students where gender=1 limit 0,3;
--简写
select * from students where gender=1 limit 3;

分页查询案例:已知每页显示m条数据,求第n页显示的数据

提示: 关键是求每页的开始行索引

查询学生表,获取第n页数据的SQL语句:

select * from students limit (n-1)*m,m

 

标签:distinct,查询,关键字,students,--,where,id,select
From: https://www.cnblogs.com/zhoubai/p/17098907.html

相关文章

  • EFore 分页查询 链式封装
    此文提供一个初步的封装思路,简化代码编写。1.先封装一个ResultSet存放查询结果:publicclassResultSet<T>{///<summary>///总记录数......
  • PHP 的glob()函数 和 关键字global
    glob()函数phpglob()函数返回匹配指定模式的文件名或目录。因此我们可以使用glob函数来查找文件,也可以实现目录的遍历。函数说明:arrayglob(string$pattern[,int......
  • 开心档-软件开发入门之MongoDB 覆盖索引查询
     作者简介:每天分享​​MongoDB教程​的学习经验、和学习笔记。  座右铭:有自制力,做事有始有终;学习能力强,愿意不断地接触学习新知识。个人主页:​​雪奈椰子的主页​​ 前......
  • DAY 253 Java transient关键字
    1.transient的作用及使用方法     我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必......
  • 开心档-软件开发入门之MongoDB 覆盖索引查询
    作者简介:每天分享MongoDB教程的学习经验、和学习笔记。  座右铭:有自制力,做事有始有终;学习能力强,愿意不断地接触学习新知识。个人主页:iOS开发上架的主页​​​......
  • MySql查询性能优化必知必会
    作为一个写业务代码的"JAVACURDBOY",具备写出高效率SQL让应用高性能访问数据库的能力非常重要。获得这个能力的过程我收获了点知识和经验,今天在这里分享出来,希望大家多多......
  • mysql查询中的技巧
    第一种:$res=Db::name('channel')->where('(fenlei=15ANDadd_time>'.strtotime("-5min").')')->fetchSql()->select();第二种:$where=[......
  • 09 数据库查询(3) | OushuDB 数据库使用入门
    表连接、组合查询大家好,本节课程我们将学习数据查询的进阶部分,主要包括表连接和组合查询。在上一节的课程中,我们尝试了在两个数据表中通过不同的条件来查询想要的数据,但是在......
  • 07 数据库查询(1) | OushuDB 数据库使用入门
    大家好,接下来我们一起学习数据查询的基础部分。 首先,什么是数据查询?从数据库中检索数据的过程或命令叫做查询。通用语法在SQL里,SELECT命令用于声明查询,通用语法如下:SE......
  • 多线程查询数据库避免重复
    contriller:packagebatch;importcom.alibaba.fastjson.JSONObject;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.......