-- 查询所有数据库
show databases ;
-- 查看当前启动的数据库
select database();
-- 查看: 当前数据库下的表
show tables;
-- 查看: 查看指定表结构
desc 表名;
-- 字符串拼接函数---concat
select concat('','');
-- 查询多个字段
select 字段1 , 字段2 ,字段3 , from 表名;
-- 查询所有字段(通配符)
select * from 表名;
-- 设置别名 as可以省略
select 字段1 as 别名1 , 字段2 as 别名2 from 表名 ;
-- 去除重复记录(distinct)
select distinct 字段列表 from 表名 ;
-- 条件查询(where)
select 字段列表 from 表名 where 条件;
-- 聚合函数
-- count():统计数量
-- max():最大值
-- min():最小值
-- avg():平均值
-- sum():求和
select 聚合函数 from 表名 ;
-- 分组查询
select 字段列表 from 表名 where条件 group by 分组字段名 having 分组后过滤条件
-- if流程控制函数
select if (条件表达式,true取值,false取值) from 表名 group by 分组字段名;
-- case流程控制函数
select case 表达式 when 值1 then 结果1 when 值2 then 结果2 else end from 表名 group by 分组字段名;
-- 排序查询
-- ASC:升序(默认值)
-- DESC:降序
select 字段列表 from 表名 where条件 order by 字段1 排序方式1,字段2 排序方式2;
-- 分页查询
-- 起始索引从0开始,起始索引= (查询页码 - 1)*每页显示记录数
-- 如果查询的是第一页数据,起始索引可以省略,简写为limit 10
select 字段列表 from 表名 limit 起始索引,查询记录数;
sql查询语法
于 2024-05-12 14:51:34 首次发布