1、条件如下:company 公司 department 部门 employee 员工
create database company;
create table company.employee5( #company. 指定了表所在的数据库
id int primary key auto_increment not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null, #如果没选择默认male
hire_date date not null, #指定字段的数据类型为日期。
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null, #指定字段的数据类型为双精度浮点数
office int,
dep_id int);
desc employee5;
1.1、插入数据:
INSERT INTO company.employee5(name, sex, hire_date, post, job_description, salary, office, dep_id) VALUES
('jack', 'male', '20230202', 'instructor', 'teach', 5000, 501, 100),
('tom', 'male', '20230203', 'instructor', 'teach', 5500, 501, 100),
('robin', 'male', '20230202', 'instructor', 'teach', 8000, 501, 100),
('alice', 'female', '20230202', 'instructor', 'teach', 7200, 501, 100),
('aofa', 'male', '20230202', 'hr', 'hrcc', 600, 502, 101),
('harry', 'male', '20230202', 'hr', NULL, 6000, 502, 101),
('emma', 'female', '20230206', 'sale', 'salecc', 20000, 503, 102),
('christine', 'female', '20230205', 'sale', 'salecc', 2200, 503, 102),
('zhuzhu', 'male', '20230205', 'sale', NULL, 2200, 503, 102),
('gougou', 'male', '20230205', 'sale', '', 2200, 503, 102);
2、简单查询:
查看所有的列: select * from 表名;
通过四则运算查询:
SELECT name, salary, salary*14 FROM employee5; #查看年薪
3、条件查询:
3.1 单条件查询 where:
查询hr部门的员工姓名:
select name,post,from employee5 where post=‘hr’;
3.2 多条件查询 AND / OR
查询hr部门的员工,并且工资是6000或者8000的员工
select name,salary from employee5 where salary=6000 or salary=8000;
查询hr部门的员工,并且工资大于1000
select name,salary from employee5 where post='hr'and salary> 1000;
查一查薪资在5000到15000 使用between 如果是不在的话就not between
select name,salary from employee5 where salary between 5000 and 15000;
3.3 使用关键字in 集合查找;
工资可能是4000,也可能是5000,还有可能是9000,怎么查 不是的话就not in()
select name,salary from employee5 where salary=4000 or salary=5000 or salary=6000 or salary=9000;
select name,salary from employee5 where salary in (4000,5000,6000,9000);
3.4、模糊查询:like
好像有个员工姓阿
select * from employee5 where name like 'al%';
#shell 是用*,mysql是用%
3.5 查询排列 order by asc/desc
例如以工资升序排列
SELECT * FROM 例如以工资降序排列 SELECT * FROM 表名 ORDER BY 工资的列名 ASC;
SELECT * FROM 例如以工资降序排列 SELECT * FROM 表名 ORDER BY 工资的列名 DESC;
select * from employee5 order by salary desc limit 5;