零基础学习数据库SQL语句之查询表中数据的DQL语句

是用来查询数据库表的记录的语句

在SQL语句中占有90%以上

也是最为复杂的操作 最为繁琐的操作

DQL语句很重要很重要

初始化数据库和表

USE dduo;

create table tb_emp(
    id int unsigned primary key auto_increment comment 'ID',
    username varchar(20) not null unique comment '用户名',
    password varchar(36) default '123456' comment '密码',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别 说明:1 男 2 女',
    image varchar(300) comment '图像',
    job tinyint unsigned comment '职位 说明:1 班主任 2 讲师 3 学工主管 ',
    entrydata date comment '入职位时间',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
)comment '员工表';

INSERT INTO tb_emp VALUES
(1,'gaochang','123456','高畅',2,'1.jpg',1,'2000-01-01','2022-10-27 17:12:32','2022-10-27 17:12:32'),
(2,'luanzengxv','123456','栾增旭',1,'2.jpg',2,'2000-01-01','2022-10-27 17:12:32','2022-10-27 17:12:32'),
(3,'liuyan','123456','刘岩',1,'3.jpg',3,'2000-01-01','2022-10-27 17:12:32','2022-10-27 17:12:32');

基本语法

USE dduo;
-- 查询指定字段name entrydate并返回
select  name ,entrydata from tb_emp;

-- 查询所有字段
#不推荐的方式 不直观 性能低 建议一个个的输入
select * from tb_emp;

-- 查询name 并起别名(姓名)
-- 字段展示时会自动变化
select name as 姓名 from tb_emp;
select name 姓名 from tb_emp;
select name '姓名' from tb_emp;
select name '姓 名' from tb_emp;

-- 查询员工一共有多少种密码 不能重复
select distinct tb_emp.password from tb_emp;

注意事项

*号表示查询所有字段 在实际开发中尽量少用 不直观而且影响效率

条件查询

在基本查询的基础上加上条件

我们主要学习的是条件的构建方式

 

USE dduo;

-- 查询姓名为高畅的员工
SELECT * from user where name='高畅';

-- 查询age小于等于20的员工
SELECT * from user where age<20;

-- 查询age是null的员工信息
SELECT * from user where age is null;
SELECT * from user where age is not null;

-- 查询age不等于19的信息
SELECT * from user where age!=19;

-- 查询指定创建日期的员工信息
SELECT * from user where creat_time >='2024-01-01 ' and creat_time<='2024-12-12' ;
SELECT * from user where creat_time between '2024-01-01 ' and'2024-12-12'  ;

-- 查询指定创建日期并且年龄为20的员工信息
SELECT *from user where  creat_time between '2024-01-01' and '2024-12-12' && age =20 ;
SELECT *from user where  creat_time between '2024-01-01' and '2024-12-12' and age =20  and name='高畅';

-- 查询年龄是19或者20的员工信息
SELECT *from user where age=19 || age=20;
SELECT *from user where age=19 or age=20;
SELECT *from user where age in (19,20);

-- 查询姓名为两个字符的员工信息 (模糊查询)
SELECT *FROM user WHERE name LIKE '__';
SELECT *FROM user WHERE name LIKE '___';

-- 查询姓氏为高的员工
SELECT *FROM user WHERE name LIKE '高%' or '高%%';

注意事项

null 和 模糊查询的两个占位符

聚合函数

为分组查询打下基础

将一列数据作为一个整体 进行纵向运算

use dduo;
-- 聚合函数

-- 统计该企业的员工数量 (非空字段)
-- 统计数据库中所有的数据量 建议使用count(*)
select count(name) from user;
select count('1') from user;
select count(*) from user;

-- 统计最早更新日期的员工
select min(user.update_time) from user;
select max(user.update_time) from user;

-- 统计更新日期的平均值
select avg(user.update_time) from user;

-- 求年龄之和
select sum(user.age) from user;

注意事项

null值不参与所有的聚合函数的运算

统计数量可以使用:count(*) count(字段) count(常量)

推荐使用 count( * )

分组查询

use dduo;

-- 根据年龄分组 统计各年龄的员工数量
select user.age,count(*) from user group by age ;

-- 先查询更新时间 再根据年龄筛选 数量大于3的年龄
select age,count(*) from user where update_time< '2025-01-01' group by age having count(*)>2;

面试题  

注意事项

排列查询

use dduo;

-- 根据更新时间 对员工进行升序排序
select *from user order by update_time asc;

-- 降序排序
select *from user order by update_time desc;

-- 根据创建时间对员工进行升序排列 如果相同 按照更新时间进行降序排序
select *from user order by creat_time asc ,update_time desc ;

注意事项

如果是多字段排序 当第一个字段值相同时 才会根据第二个字段进行排序

分页查询

use dduo;

-- 分页查询

-- 1.从起始索引0开始 开始查询员工数据 每页展示1条记录
select *from user limit 0,1;

-- 查询第1页 员工数据每页展示2条记录
select *from user limit 2,2;

-- 查询第2页 员工数据每页展示2条记录
select *from user limit 4,2;

-- 查询第3页 员工数据每页展示2条记录
select *from user limit 6,2;

-- 查询第4页 员工数据每页展示1条记录
select *from user limit 8,1;

起始索引 = (页码-1) * 每页展示的记录数

将来开发关系型数据库的时候

前端并不会把起始索引传递过来 而是传递页码

我们要换算成索引 在MySQL中书写SQL语句

相关推荐

  1. SQL语句详解四-DQL(数据查询语言-多查询二)

    2024-05-04 17:34:01       65 阅读
  2. 数据库SQL语句DML&&DQL

    2024-05-04 17:34:01       44 阅读
  3. 基本DQL语句-单查询

    2024-05-04 17:34:01       25 阅读
  4. SQL语法DQL):SELECT 多查询查询

    2024-05-04 17:34:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 17:34:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 17:34:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 17:34:01       82 阅读
  4. Python语言-面向对象

    2024-05-04 17:34:01       91 阅读

热门阅读

  1. Go-变量

    Go-变量

    2024-05-04 17:34:01      63 阅读
  2. 微信小程序开发中的多国语言支持和国际化

    2024-05-04 17:34:01       33 阅读
  3. 桌面运维岗面试三十问

    2024-05-04 17:34:01       20 阅读
  4. 2024.04.11校招 实习 内推 面经

    2024-05-04 17:34:01       39 阅读
  5. Python 正则表达式2 语法基础

    2024-05-04 17:34:01       28 阅读
  6. 上海计算机学会2020年7月月赛C++丙组T4数字验证

    2024-05-04 17:34:01       32 阅读
  7. 【protobuf】protobuf 开发 (二)

    2024-05-04 17:34:01       35 阅读
  8. 卷积神经网络

    2024-05-04 17:34:01       29 阅读
  9. typescript 学习笔记

    2024-05-04 17:34:01       32 阅读
  10. vue如何实现异步组件

    2024-05-04 17:34:01       36 阅读
  11. 【开发技巧】青龙面板cookie过期

    2024-05-04 17:34:01       33 阅读
  12. DataV的轮播表后端返回的数据处理

    2024-05-04 17:34:01       40 阅读