表查询关键字

8.17内容整理和概述

今日内容概要

内容目录

  • SQL语句补充
  • 表查询关键字
  • 连表操作

SQL语句补充

1.修改表名:alter table 表名 rename 新表名;
2.新增字段:
	2.1 alter table 表名 add 字段名 字段类型(数字) 约束条件;
	2.2 alter table 表名 add 字段名 字段类型(数字) 约束条件 after 已经存在的字段;
	2.3 alter table 表名 add 字段名 字段类型(数字) 约束条件 first;
3.修改字段:
	3.1 alter table 表名 change 旧字段 新字段 字段类型(数字) 约束条件;
 	3.2 alter table 表名 modify 字段名 新的字段类型(数字) 约束条件;
4.删除字段:alter table 表名 drop 字段名;

表查询关键字

select + 字段:自定义查询表中字段对应的数据

from:指定操作的对象(到底是哪张表 也可能是多张)

where:筛选,类似python的if
	例子:
	1.where id >= 3 and id <= 6;
	2.where id between 3 and 6;  
	3.where salary = 20000 or salary = 18000 ;
	4.where salary in (20000,18000,17000);
	5.where name like '%o%';# 模糊查询	
	6.where name like '____'; # 四个字符组成
	7.where post_comment = NULL;  # 查询为空!
    
group by:分组(规定select取值范围)
	运用条件:
        1.max:最大值
        2.min:最小值
        3.sum:总和
        4.count:计数 
        5.avg:平均
	例子:
        1.select post as '部门',max(salary) as '最高工资' from emp group by post; # as 给字段重新命名(as可有可无)
	group_concat:显示分组外字段,拼接字符串的作用 # group_concat(name,": ",salary)
    
having:过滤
	区别:where用于分组之前的筛选 而having用于分组之后的筛选

order by:排序(默认升序)
	升序:asc
	降序:desc
	例子:select * from emp order by age desc,salary asc; 
	# 先按照age降序排,在age相同的情况下再按照salary升序排

limit:分页
	例子:select * from emp limit 起始位置,数量;

regexp:正则
	例子:select * from emp where name regexp '^j.*(n|y)$';

连表操作

1.inner join(内连接):只拼接两边都有的字段数据
2.left join(左连接):以左表为基准 展示所有的数据 没有对应则NULL填充
3.right join(右连接):以右表为基准 展示所有的数据 没有对应则NULL填充
4.union(全连接):		  
posted @ 2022-08-18 20:20  维生素Z  阅读(9)  评论(0)    收藏  举报