Coalesce将空值换为其他值
coalesce(col1,col2,col3..)
Delete 删除语句
delete from 表名
where 条件;
注意:delete 语句如果没有加where条件,将会把表中所有数据删除
count(*)表示当前表中一共有多少记录
select count(*) from 表名;
like 语句 一般配合通配符使用
通配符%:表示0,1或者更多
通配符_:表示单一数字或字符
select * from 表名
where 列名 like ‘XX%’;
select * from 表名
where 列名 like ‘X_X_’
(X无实意,代表字符或数字)
limit 语句 限制
select * from 表名 limit n;
其中n指的是返回表中前n条数据
select * from 表名 limit m,n;
从第m+1条记录开始,返回n条数据
【SQL中第一条索引是1,python索引是0开始】
order by 排序
ASC 升序
DESC 降序
select 列名 from 表名
where 条件
order by 列名 DESC;
【order by 语句后面出现的列名,可以不在select语句中】
随机选中表中数据
select * from 表名
order by rand()
limit N;
group by 分组 常与聚合函数一起用
常用聚合函数:
count() 总数
sum()求和
avg()平均值
min()最小值
max()最大值
select 列名1,列名2,列名n,聚合函数(表达式) from 表名
where 条件
group by 列名1, 列名2....列名n
order by 。。
【注意】
1.group by 语句必须放在where语句后,order by 语句之前
2.group by之后的列,如果不出现在聚合函数表达式中,则必须出现在group by语句中
having 语句:对group by 产生的分组进行筛选
【注意】
1.having 语句通常与group by联合使用,用来过滤由group by语句返回的记录集合
2.having 语句弥补了where语句不能与聚合函数联合使用的不足
select 列名1,列名2...列名n,聚合函数(表达式) from 表名
where 条件
group by 列名1,列名2,列名n
having 条件1,条件2,条件n
distinct 去除重复值,提取唯一记录,与select语句一起使用
select distinct 列名 from 表名
where 条件;
给表或列临时起一个别名
select 列名 from 表名 as 新表名;
select 列名 as 新列名 from 表名;