在表格的增删改查中,查的内容是最多的,包括group by ,join,limit,union,alter,排序都是服务于查的html
#sql语句数据行操做补充 #增长: #insert into table_name(字段1,字段2) values('属性1','属性2'),('属性1','属性2') 插入多行数据 #insert into t1(字段1,字段2) select 字段1,字段2 from t2; (某张表的某几列数据选择出来,插入) #删除:delete from table_name [where id =/!=/>/< /<> 2] and/or name='啥啥啥' #truncate table t1; #改: update t1 set name='gkx' ,age=19 where id>12 and name='x' #能够同时改多个字段 ''' #查:内容较多: # 查全部,查部分字段,后面跟 where条件,查完后排序 #select id,name from t1 where id>10 and name = 'aaa' #select id as setid ,name as setname from t1 where id>10 and name = 'aaa' # as 别名 #as 别名 #select name as cname ,age,1 from t1; #这里的 1表示 增长一列,列名为1,该列全部行属性也都为1 #select * from t1 where id in (1,2,5); #若是id在 1,2,5就选择出来 #select * from t1 where id not in (1,2,5); #id除了1,2,5其余都选择出来 #select * from t1 where id in (select id from t2 where id>2) ;# 根据选择来选择 #select * from t1 where id between 5 and 12; #闭区间 5和12也能取到 #select * from dmsxb where b_dm in (select b_dm from dmsxb group by b_dm having count(*)>1) ##还能够选择查询语句,不过这个语句结果须要为一个常量 select age,name,(select count(1) from tb) from tb1; #习题17和21 #条件 ## case when 条件 then 【字段或者默认值】 else 字段 END #select course_id,max(num),min(num),cname, # case when min(num) < 10 THEN 0 else min(num) end from score left join course on score.course_id = course.cid group by course_id; #if 三元运算 # select course_id,cname,tname,avg(if(isnull(score.num),0,score.num)) from score #通配符: #下划线 : 表示后边只能匹配一位,select * from tb12 where name like "a_" #百分号 : select * from tb12 where name like "a%" ''' #limit:好比百度搜索,一次只显示一页,放置C/S端压力 #select *from t1 limit 10; #显示前十条 至关于select *from t1 limit 0,10; #select * from t1 limit 4,5; #从第4行开始的5行 (不包含第4行) #select * from t1 limit 5 offset 4 #从第4行开始的五行 #实现分页: #page = int(input('page')) select * from limit (page-1)*10 , 10; 前者为起始位置 #select * from t1 order by id desc limit 10; (后10行:先降序排列,再选前十行) ''' limit 数值越大越慢,好比 limit 300000,10 它会先扫描30万行,而后再取下10行 ''' #排序: #select * from t1; #默认是从小到大排 即 asc ----> ascending #select * from t1 order by 列名 desc; 即 desc ----> descending #select * from t1 order by 列1 desc,列2 asc ; #根据列1从大到小排列,若是相同按列2从小到大排列
# Mysql增长主键或者更改表的列为主键的sql语句 # 添加表字段 # alter table table1 add transactor varchar(10) not Null; # alter table table1 add id int unsigned not Null auto_increment primary key # 修改某个表的字段类型及指定为空或非空 # alter table 表名称 change 字段名称 字段名称 字段类型 [是否容许非空]; # alter table 表名称 modify 字段名称 字段类型 [是否容许非空]; # 修改某个表的字段名称及指定为空或非空 # alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否容许非空 # 删除某一字段 # ALTER TABLE mytable DROP 字段 名; # # 添加惟一键 # ALTER TABLE `test2` ADD UNIQUE ( `userid`) # 修改主键 # ALTER TABLE `test2` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `id` ) # # 增长索引 # ALTER TABLE `test2` ADD INDEX ( `id` ) #修改字段类型,并设置主键 # ALTER TABLE `category ` MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST ,ADD PRIMARY KEY (`id`); #建表后建立惟一索引: # create unique index UK_student_name on student (name); # 建表后添加约束 # alter table student add constraint uk_student_name unique (name); #修改表的列属性 # alter table 表名 change 原列名 新列名 类型; --修改表的列属性名 # alter table 表名 modify 列名 类型 ; --修改表的类类型 # alter table 表名 drop 列名; --删除表的某一列 # alter table 表名 add 列名 类型; --添加某一列 # alter table 表名 rename 新表名; --修改表名 ''' #1. select now(); 打印当前时间 #2. desc table_name; 查看字段的属性 #3. show create table table_name ; 查看表的建立代码 #3. show create table table_name \G ; 竖着看,查看表的建立代码
分组的实现,注意事项,总结mysql
#分组 group by: #经过group by 按照所选列分组显示,按所选列分组,所选列若是 不重复 的项有4个,就分红4组 # select part_id from t1 group by part_id; #错误:select part_id,name from t1 group by part_id; #这是不行的,由于它按part_id分组,聚合在一块儿,此时不知道 相同part_id可是不一样name的,该如何取舍哪一个name因此会报错 #能够这么写: # select part_id,max(id) from t1 group by part_id; #当part_id 分组完后,留下id列中,相同part_id里,id最大的那行 # select part_id,min(id) from t1 group by part_id; #结合count等聚合函数使用: 能够用as更名哦!!! #select count(id),part_id from t1 group by part_id; 用part_di分组,新增一列count(id),计算每组id数 #相似还有 count,max,min,sum,avg(好比avg(分数),取每一个分组中,分数这个列的平均值i) # select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid #若是对于聚合函数结果进行二次筛选时,必须使用having! #select num from 表 group by num having max(id) > 10 #选择出最大id大于10的,按num分组的,全部分组 #加条件: select part_id from user where id !=1 group by part_id having count(*)>1; #··特别的:group by 必须在where以后,order by以前 #select num,nid from 表 where nid > 10 group by num,nid order nid desc #group 多列, 用两列作分组, #select num,nid from 表 where nid > 10 group by num,nid order nid desc #选出nid大于10的,num,nid两列分组后的 num,nid。并用nid排序 #好比:姓名和部门,用姓名和部门进行分组,选出年龄大于30的,同姓名同部门的全部用户 #总结: #group by 是用来分组的,把某一列都聚合到同一组,因此select出的字段要不就是这一列,要不要使用函数,要不和分组字段是惟一对应关系的 #聚合函数有:(不能用where) #count(列名) max(列名) min(列名) sum(列名) avg(列名) #能够用as更名哦,select count(id) as count_id,part_id from user group by part_id having count(*)>1; #对于聚合函数的条件筛选不能用where,要用 having #group by能够多列进行联合分组 #group by要写在where 后面,order by 前面 # -- 七、查询学过“1”课程而且也学过编号“2”课程的同窗的学号、姓名; # -- score和student连表,找出全部选过1,2课程的学号,而且按学号分组 # select student.sid,student.sname from score left join student on score.student_id = student.sid where course_id in (1,2) group by student_id; #注意在这个例子中 学号和姓名是惟一对应的,因此能够直接选姓名,而不用聚合函数
连表操做sql
''' 连表最好是外键中的子表父表连 连表是支持 不一样数据类型之间链接的,可是这样子操做没意义 ''' #连表: #两张表不要紧,则不显示: # select A.num, A.name, B.name from A,B Where A.nid = B.nid #建议不要用这种,不直观 # select A.num, A.name, B.name from A inner join B on A.nid = B.nid #其实只要记一个left join就好,把表换位置就好啦 # left join(左联接) A表全部显示,若是B中无对应关系,则值为null #语法:select * from A left join B on A.aID = B.bID #展现左边表全部的值,依据条件链接右边表, 右边表有,左边表没有的值,将在结果中右边置为空 # right join(右联接) B表全部显示,若是B中无对应关系,则值为null #语法:select * from A right join B on A.aID = B.bID #展现右边表全部的,依据条件链接左边表, 左边表有,右边表没有的值,将在结果中左边置为空 # inner join(等值链接) 只返回两个表中联结字段相等的行 #语法:select * from A inner join B on A.aID = B.bID #只展现两边表有的值,没有的值不设置null,直接不显示 #···无对应关系则不显示!!!! #三者区别 : https://www.cnblogs.com/pcjim/articles/799302.html #连多张表: 图在:20181007 mysql连表 #select * from 表A left join 表B on A.ID = B.ID # left join 表C on A.ID = C.ID # left join 表D on A.ID = D.ID # select 的时候,字段能够 表示为 表名.字段名 # 当表链接后,接下俩的join 就能够随意使用 #举例: # -- 20、课程平均分从高到低显示(显示任课老师); # select course_id,cname,tname,avg(num) from score # left join course on score.course_id = course.cid # left join teacher on course.teacher_id = teacher.tid # group by course_id order by avg(num) desc;
#组合 # 组合,自动处理重合 # select nickname from A union select name from B # # 组合,不处理重合 # select nickname from A union all select name from B
临时表,distinct去重以及和group by的区别ide
#临时表 # (select * from t1 where id > 2) as 临时表名 #用法: # select sid from (select * from score where num>60) as b;
# https://blog.csdn.net/ljl890705/article/details/70602442 #distinct简单来讲就是用来去重的,而group by的设计目的则是用来聚合统计的,二者在可以实现的功能上有些相同之处,但应该仔细区分,由于用错场景的话,效率相差能够倍计。 # ·····单纯的去重操做使用distinct,速度是快于group by的。 # # distinct (说是效率不高) # distinct支持单列、多列的去重方式。 # 单列去重的方式简明易懂,即相同值只保留1个。 # 多列的去重则是根据指定的去重的列信息来进行,即只有全部指定的列信息都相同,才会被认为是重复的信息。 #单列去重: #select distinct(mobile) from talk_test; # 多列去重: # mysql> select distinct name,mobile from talk_test; #结合count使用: # select count(distinct col) from A; # select count(1) from (select 1 from A group by col) alias; # 两个极端: # 1.数据列的全部数据都同样,即去重计数的结果为1时,用distinct最佳 # 2.若是数据列惟一,没有相同数值,用group 最好