数据的增删改查

#数据的增删改查
#插入表记录(即往表里插入数据)
#方式1
#插入一条:
#insert into 表名(字段)
#values(字段对应的值);
#插入多条:
#insert into 表名
#values(字段对应的值),
#(字段对应的值);
#方式2
#insert into 表名 set 字段=;

#修改表记录
#update 表名 set 字段=[where 语句];
#删除表记录

#delete 只能用来删除一行记录
#delete只能删除表中的内容,不能删除表本身
#truncate table 此语句首先摧毁表,在新建表,数据不能恢复
#格式:
#delete from 表名; #清空表里面的所有数据
#truncate table 表名;

#查询表记录(数据查询)
#格式:
#select [all|distinct] <列表达式>
#from 表名
#where <条件表达式>
#group by<列名>[having <条件表达式>]
#order by<列名>[asc|desc];

#说明:
#distinct代表去掉重复行
#all显示所有,默认显示所有
#group by 分组
#order by 排序(asc升序,默认为此项; desc降序)

#使用别名来显示
#格式:字段 as 别名 或者 字段 别名
#实例:
#select name as 姓名,js as 成绩 from examresult;
#或者
#select name 姓名,js 成绩 from examresult;

#where子句
#比较:=>,<,>=,<=,!=<>,!>,!< ,not+比较运算符
#范围:between and not between and
#确定集合: in, not in (用来查找属性值属于指定集合的元组)
#字符匹配:like, not like
#空值:is null, is not null
#多重条件:and, or, not

#实例:
#select name,js from examresult where js between 80 and 90;
#select name,js from examresult where js not between 80 and 90;

#select id,name from exam where dept in ("CS");
#select id,name from exam where dept not in ("CS");

# select * from exam where js is null;
# select * from exam where is not null;

#select * from exam where js=90 and dept="CS";
#select * from exam where js=90 or dept="CS";
#select * from exam where not js=90;

#字符串匹配:
#通配符:
# %(百分号)代表任意长度的字符串(长度可以为0
# _(下横线)代表单个任意字符
#格式:
#like'<匹配串>['escape'<换码字符>']
#如果查询的字符串本身具有通配符,就要使用escape'<换码字符>'
#实例:
#select * from exam where name like 'a%';
#select * from exam where name like '_';

#group by分组查询(按位置字段筛选)
#实例:
# select * from exam group by dept;
# 练习:对成绩表按照名字分组后,显示每一类名字的js的分数。
# select name,sum(js)from ExamResult group by name;
# 练习:对成绩表按照名字分组后,显示每一类名字的Django的分数总和>150的类名字和Django总分
# select name,sum(Django) from ExamResult group by name having sum(Django)>150;

#having where
#where语句只能用在分组之前的筛选,having可以用在分组之后的筛选
#使用where语句的地方都可以用having替换
#having可以用聚合函数,where不可以
#练习:对成绩表按名字分组后,显示除了yuan这一组以外的每一类名字的Django的分数总和>150的类名字和Django总分
#select name,sum(Django)from ExamResult where name !="yuan"group by name having sum(Django)>150;

#聚集函数(聚集函数只能用于select子句和group by中的having子句)
#count([distinct|all]<列名>):统计一列中值的个数
#count(*):统计所有行 count(字段) 不统计null
#max([distinct|all]<列名>):求一列值中的最大值
#min([distinct|all]<列名>):求一列值中的最小值
#avg([distinct|all]<列名>):求一列值的平均值
#sum([distinct|all]<列名>):求一列值的总和


原文地址:https://www.cnblogs.com/shadowfolk/p/14706119.html