mysql指令

    xiaoxiao2022-07-04  168

    1.取别名指令 给列取别名:select 列1 别名1,列2 别名2,from 表名; 给表取别名:select * from 表名 as 表别名; 2.去重 去除行重复:select distinct 列1 from 表名; 3.where条件查询 比较运算符 大于> 小于< 大于等于>= 小于等于<= 不等于!= 逻辑运算符 且and 或or 非! 模糊查询 like模糊查询关键字 匹配多个字符% 匹配单个字符_ select * from 表名 where 列名 like 条件 范围查询 between…and… select * from 表名 where 列名 between 开始范围 and 结束范围(不能适用于文字) select * from 表名 where 列名 in(查找值1,查找值2);(可以适用于文字) 空判断 判断为空 is null 判断不为空 is not null select * from 表名 where 列名 is null select * from 表名 where 列名 is not null 排序 asc 升序 desc 降序 order by 排序关键字 select * from 表名 order by 列 asc; select * from 表名 order by 列 desc; #如果出现按条件排序,先按条件查询在排序执行效率高 分页查询 (n-1)*m,m 查询范围计算方法 select * from 表名 limit 开始范围 结束范围 聚合函数 count()计算总行数 max()计算最大值 min()计算最小值 sum()计算总和 avg()算平均值 select count(列名),max(列名),min(列名),sum(列名),avg(列名) from 表名 分组查询 按指定行分组:select 列1 from 表名 group by 列1;但不能查看其他行 查看分组后的其他行数据: select 列1,group_concat(显示列) from 表名 group by 列1; 分组+聚合函数: select 列1,count(),max(),min(),sum(),avg() from 表名 group by 列1; 汇总:select 列1 count(),max(),min(),sum(),avg()from 表名 group by 列1 with rollup; 统计个数:select 列1 count(统计信息) from 表名 group by 列1 having count(统计信息)>个数; 查询连接: 内连接:取两个表交集 select * from 表1 inner join 表2 on 表1.字段1=表2.字段2;查看部分字段时要表名.字段名 左连接: 以左表为主条件查询右表,右表数据不存在用null填充 select *from 表1 left join 表2 on 表1.字段1 = 表2.字段2;后面可加条件 右连接: 以右表为主条件查询左表,左表数据不存在用null填充 select * from 表1 right join 表2 on 表1.字段1 = 表2.字段2; 左连接替换右连接,要把左右两个表顺序换一下 自连接 自己连接自己,连接方式可使用内链接,左链接,右链接 一张表定以两个别名,当成两张表使用

    子查询 查询嵌套 将一条查询语句连接到主查询中去 select * from 表名 where c_age > (select avg(c_age) from t_student);

    最新回复(0)