
CREATE TABLE worker (部门号 int(11) NOT NULL,职工号 int(11) NOT NULL,工作时间 date NOT NULL,工资 float(8,2) NOT NULL,政治面貌 varchar(10) NOT NULL DEFAULT 群众,姓名 varchar(20) NOT NULL,出生日期 date NOT NULL,PRIMARY KEY (职工号)) ;INSERT INTO worker VALUES (101, 1001, 2015-5-4, 3500.00, 群众, 张三, 1990-7-1);INSERT INTO worker VALUES (101, 1002, 2017-2-6, 3200.00, 团员, 李四, 1997-2-8);INSERT INTO worker VALUES (102, 1003, 2011-1-4, 8500.00, 党员, 王亮, 1983-6-8);INSERT INTO worker VALUES (102, 1004, 2016-10-10, 5500.00, 群众, 赵六, 1994-9-5);INSERT INTO worker VALUES (102, 1005, 2014-4-1, 4800.00, 党员, 钱七, 1992-12-30);INSERT INTO worker VALUES (102, 1006, 2017-5-5, 4500.00, 党员, 孙八, 1996-9-2);第一步了解增删减查的语句增:添加数据insert指令注意SQL语句不区分大小写也就是大小写一致# 语法结构 insert into 表名称[(字段1 [,字段2, ……])] {values|value}(字段值, [……]) [,()]; # 注意如果主键自增可以使用null或者default关键字填充 # 其他字段如果存在默认值则使用default关键字填充update指令update 表名称 set 字段名称新的值 [,字段xxx [,……] [where 条件];replace语句replace into 表名称[(字段1 [,字段2, ……])] {values|value}(字段值, [……]) [,()];replace语句结构和insert的语法结构一模一样注意replace 的sql语句是集更新和插入于一体的一个SQL。如果插入的数据不存在主键、unqiue修饰的字段执行insert执行如果插入的数据存在主键、unqiue修饰的字段则先执行删除语句再执行insert语句。删删除数据DELETE指令注意where关键字必须存在条件时才能出现SQL中单引号和双引号是一样的delete from 表名称 [where 一个或者多个条件];查作业1、显示所有职工的基本信息。2、查询所有职工所属部门的部门号不显示重复的部门号。3、求出所有职工的人数。4、列出最高工和最低工资。5、列出职工的平均工资和总工资。6、创建一个只有职工号、姓名和参加工作的新表名为工作日期表。7、显示所有女职工的年龄。8、列出所有姓刘的职工的职工号、姓名和出生日期。9、列出1960年以前出生的职工的姓名、参加工作日期。10、列出工资在10002000之间的所有职工姓名。11、列出所有陈姓和李姓的职工姓名。12、列出所有部门号为2和3的职工号、姓名、党员否。13、将职工表worker中的职工按出生的先后顺序排序。14、显示工资最高的前3名职工的职工号和姓名。15、求出各部门党员的人数。16、统计各部门的工资和平均工资17、列出总人数大于4的部门号和总人数。select条件查询select * from t_user; select id, name, age from t_user; # 等值条件 select address from t_user where id 1; select age from user where name刘建宏; # 关系条件 -- ! # 判断是否为空 # is关键字 is null is not null select * from user where address is NULL; select * from user where address is not NULL; # 符号 # 充当等号的作用 # 可以用来判断空 select * from user where age 18; select * from user where gender null; # 多个条件 # 逻辑运算符 # and 并且 # or 或者 # not 不是取反 select * from user where age 18 and gender 男 and id 10; select * from user where age 18 or gender 女; select name from user where gender is not null; select name from user where not gender 男; # 范围 update user set age 25 where id 6 and id 10; update user set age 16 where id between 12 and 15; select * from user where not (age 20 or age 25); # 注意条件的执行顺序问题 select * from user where not age 20 or age 25; # 列举 in not in select * from user where id in (1,3,8,10); select * from user where id not in (1,3,8,10); # 去重效果 # 使用distinct关键字去掉重复值 select distinct age from user;日期的格式使用字符串来表示yyyy-mm-dd 如 2000-03-30 ‘yyyy/mm/dd’ 如 2024/5/11 hh:mm:ss 如 ‘12:12:21’ yyyyy-mm-dd hh:mm:ss ‘2020-3-4 16:05:30’模糊查询使用like关键字进行模糊匹配%匹配0到多位_匹配一个具体的位select * from user where name like %亮; select * from user where name like %亮%; select * from user where name like %张%; select * from user where name like 张%; # 表示第二个字是“绣” select * from user where name like _绣%;正则查询select * from user where name regexp ^张; select * from user where name regexp 亮$;复杂查询分组查询将数据相同的会放在同一个组中也就是不会出现重复数据。往往是用来做数据分析。select 字段 from 表名 [where 条件] group by 字段 [, 字段 [,……]] 案例-- 统计不同性别的人数 select count(gender), gender from user group by gender; having语句having语句是配合分组使用是分组后的筛选select 字段 from 表名 [where 条件] group by 字段 [, 字段 [,……]] having 筛选条件案例select gender, count(gender) from user group by gender having count(gender) 8; select gender, count(gender) from user where age 18 group by gender having count(gender) 5;聚会函数-count统计数据select count(id) from user; select count(gender) from user; select count(id) from user; select count(1) from user; select count(1) from user where gender男;排序order by 字段 [{asc | desc }]如果存在排序必须是在分组之后select 字段 from 表名 [where 条件] [group by 字段 [, 字段 [,……]] ] [having 筛选条件] order by 字段 [{asc | desc }] [, 字段 [{asc | desc }]