MySQL学习day04(一)

DQL学习(Data Query Language数据查询语言)

DQL-语法:

select
字段列表
from
表名列表
where
条件列表
group by
分组字段列表
having
分组后条件别表
order by
排序字段列表
limit
分页参数

  • 基本查询
  • 条件查询(where
  • 聚合函数(count、max、min、avg、sum
  • 分组查询(group by
  • 排序查询(order by
  • 分页查询(limit

1)基本查询

查询多个字段:

  1. select 字段1,字段2,字段3,...from 表名;
  2. select * from 表名;

设置别名:

  1. select 字段1 [as 别名1],字段2[as 别名2],字段3[as 别名3],...from 表名;

  2. select 字段1[别名1],字段2[别名2],字段3[别名3],... from 表名;

去除重复记录:

select distinct 字段列表 from 表名;

转义:

select * from 表明 where name like '/_张三' escape '/';
/之后的_不作为通配符

2)条件查询
语法:

select 字段列表 from 表名 where 条件列表;

条件:

比较运算符 功能
> 大于
>= 大于等于
< 小于
<= 小于等于
= 等于
<>或 != 不等于
between…and… 在某个范围内(含最小,最大值)
in(…) 在in之后的列表中的值,多选一
like 占位符 模糊匹配(_匹配单个字符,%匹配任意个字符)
is null 是null
逻辑运算符 功能
and 或 && 并且(多个条件同时成立)
or 或 ‘//’ 或者(多个条件任意一个成立)
not 或 ! 非,不是

例子:

1、年龄等于30
select * from employee where age = 30;
2、年龄小于30
select * from employee where age < 30;
3、小于等于
select * from employee where age <= 30;
4、没有身份证
select * from employee where idcard;
select * from employee where idcard is not null;
5、不等于
select * from employee where age != 30;
6、年龄在20到30之间
select * from employee where age between 20 and 30;
select * from employee where age >= 20 and age <= 30;
7、下面语句不报错,但查不到任何信息
select * from employee where age between 30 and 20;
8、性别为女且年龄小于30
select * from employee where age < 30 and gender = '女';
9、年龄等于25或30或35
select * from employee where age = 25 or age = 30 or age = 35;
select * from employee where age in (25, 30, 35);
10、姓名为两个字
select * from employee where name like '__';
11、身份证最后为X
select * from employee where idcard like '%X';

3)聚合查询(聚合函数)
常见聚合函数:

函数 功能
count 统计数量
max 最大值
min 最小值
avg 平均值
sum 求和

语法:

select 聚合函数(字段列表)from 表名;

注意:null值不参与所有聚合函数运算

例子:

select count(id)from employee where workaddress = "广东省";

相关推荐

  1. MySQL学习day04

    2023-12-08 01:36:02       55 阅读
  2. 【软测学习笔记】MySQL入门Day04

    2023-12-08 01:36:02       30 阅读
  3. 数据库Mysql学习day01课堂笔记

    2023-12-08 01:36:02       52 阅读
  4. 【软测学习笔记】MySQL入门Day01

    2023-12-08 01:36:02       34 阅读

最近更新

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

    2023-12-08 01:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 01:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 01:36:02       82 阅读
  4. Python语言-面向对象

    2023-12-08 01:36:02       91 阅读

热门阅读

  1. qt反射基础

    2023-12-08 01:36:02       49 阅读
  2. android 13.0 framework禁用系统所有通知

    2023-12-08 01:36:02       57 阅读
  3. Linux下超轻量级Rust开发环境搭建:一、安装Rust

    2023-12-08 01:36:02       59 阅读
  4. python pandas dataframe常用数据处理总结

    2023-12-08 01:36:02       57 阅读
  5. 纯C读取文件实现解析H264裸流每一帧数据

    2023-12-08 01:36:02       66 阅读
  6. Redisson

    2023-12-08 01:36:02       69 阅读
  7. 算法 拓扑序列

    2023-12-08 01:36:02       51 阅读
  8. Redis默认序列化方式乱码原因及解决办法

    2023-12-08 01:36:02       61 阅读
  9. 计算机网络——传输层

    2023-12-08 01:36:02       60 阅读
  10. python模块 — json

    2023-12-08 01:36:02       65 阅读
  11. TCP_NODELAY与TCP通信效率

    2023-12-08 01:36:02       61 阅读
  12. 【算法】合并K个升序链表

    2023-12-08 01:36:02       58 阅读