2019独角兽企业重金招聘Python工程师标准>>>
-----创建表----
----------表格头英文换中文显示select name as '名字',age 年龄,class from student
-----if not exists判断表存在否--字符串用char也行---
--如果用自增长,只能用包装类型integer,不能用int--
create table if not exists student(id Integer primary key autoincrement not null, name String not null, sex String not null, age int not null, hight real , class String
)
------用逗号分隔,最后一个字段不用写-----
---删除表------
drop table student
------插入一条数据------
insert into student (name,sex,age,hight,class) values('张三','男',21,178,'cc111')
------插入多条数据------
insert into student (name,sex,age,hight,class) values('李四','男',23,170,'cc111'),('王五','女',20,173,'cc111')
,('赵六','男',22,179,'cc112'), ('朱七','女',24,171,'cc112')
------查询所有数据------
select * from student
-------------------
select * from student where age=22
------修改数据------
---- update 表名 set 字段 =-加条件 where ---
update student set hight=null where id=5
-----班级为cc111,男,hight不为null,则每个高度都增长1
update student set hight=hight+1 where class='cc111' and sex='女' and hight not null
---------------凡是cc111班年龄减1----update student set age=age-1 where class='cc111'
-------------凡是cc111班或者性别为女的则高度+1----------------------update student set hight=hight+1 where class='cc111' or sex='女'
--------修改名字的第一个字为王的年龄减1岁
update student set age =age-1 where name like '王%'
-----------修改名字为张三或者为李四的班级为cc112----------------
update student set class='cc112' where name in('张三','李四')
----删除所有
delete from student
-------删除名字含朱的
delete from student where name like '%朱%'
--查询所有
select * from student
---女的
select * from student where sex='女'
------查询cc111班的人的名字
select name from student where class='cc111'
--查询大于21,小于23的人
select * from student where age>=21 and age<=23
---查询名字含朱的
select * from student where name like '%朱%'
---------查询cc111班女生,并且按年龄从小到大排序- order by,降序 desc,默认就是升序-
select * from student where sex='女' and class='cc111' order by age desc
-------------查询所有女生,并且按年龄从大到小排序-,只显示前3个----
----limit 偏移量0表示第一个,显示的个数-----
select * from student where sex='女' order by age desc limit 0,2
---查询出cc112班的总人数--------
select count(*) from student where class='cc112'
------查询出cc112班的所有人的年龄总和---------------
select sum(age) from student where class='cc112'
-------查询出cc111班的平均年龄-------------
select avg(age) from student where class='cc112'
-----查询所有的班级的男性人数,并且按班级进行分组 group by ---------
select class,count(*) from student where sex='男' group by class
-----查询cc112班年龄最大的学员-----------
select max(age),name from student where class='cc112'
-----查询班上的所有学员,按班级进行分组,再进行降序,并且只列出总人数大于等于1个班级-----------
select class,count(*) from student
group by class having count(*)>=1 order by count(*) desc