MySQL做题笔记

1 null

null不参与比较,是否等于null要单独列出

不能使用 = null , 应该使用 is null is not null

sql 复制代码
select name 
from Customer 
where referee_id <> 2 or  referee_id is null;

2 distinct

显示不重复的数据

3 char_length()

计算字符串长度

4 left join

包含左侧的所有行,即使右侧没有对应数据

5 cross join

交叉连接

join 等价于inner join,不用关联条件的join等价于cross join.

6 datediff()

计算日期差值

7 max()

8 avg()

9 round(x,2)

x保留两位小数

10 ifnull(a,b)

ifnull(a,b) a如果为null,就返回b

avg(action = 'confirmed') :

AVG(判断条件) = "判断条件"为True的记录数 / 总记录数

if(判断条件,1,0) : 满足返回1,不满足返回0

11 <> 替代 !=

12 mod(a,b) a%b

13 sum(a*b)

每一行分别计算a*b,然后计算和

14 count()

select count(1) from users : 统计user中非null的行数

select count(distinct player_id ) from activity:统计player_id不重复的行数

count()作用:

统计符合条件的记录中,指定的条件不为null的数量

效率:

count(*)>count(1)>count(主键字段)>count(字段)

如果是count(1):

1永远满足,结果是总行数

count(if(state = 'approved', 1, null)):

count()中也可包含表达式,统计行数时,不满足条件要设为null,而不是0

15 if()

if(rating < 3, 1, 0)

sum(if(rating < 3, 1, 0)) 统计所有rating<3的行数

16 date_format()

date_format(date, '%Y-%m') 格式化日期,按照2018-02形式显示

相关推荐
h***34633 小时前
【MySQL】表的基本操作
数据库·mysql·oracle
AA陈超4 小时前
UE5笔记:GetWorld()->SpawnActorDeferred()
c++·笔记·学习·ue5·虚幻引擎
n***63274 小时前
MySQL数据库的数据文件保存在哪?MySQL数据存在哪里
数据库·mysql
月上柳青5 小时前
OpenWrt系统上配置batman-adv快速开始与配置详解
开发语言·mysql·php
t***26595 小时前
【大数据】MySQL与Elasticsearch的对比分析:如何选择适合的查询解决方案
大数据·mysql·elasticsearch
生椰拿铁You5 小时前
openxlpy学习笔记
笔记·学习
AA陈超5 小时前
ASC学习笔记0025:移除所有属性集
c++·笔记·学习·ue5·虚幻引擎
QT 小鲜肉6 小时前
【Linux常用命令大全】在 Linux 系统下 Git + Vim编辑器常用指令完全指南(亲测有效)
linux·开发语言·c++·笔记·git·编辑器·vim
桦07 小时前
【MySQL】视图
数据库·mysql
java_logo9 小时前
Kubernetes Dashboard Docker 容器化部署指南
运维·mysql·docker·云原生·容器·kubernetes·php