7,sql 基本的创建库表查询数据
-- 创建库
CREATE DATABASE IF NOT EXISTS test04_dql;
切换到这个库
USE test04_dql;
-- 创建员工表
DROP TABLE IF EXISTS `t_employee`;
---创建字段
CREATE TABLE `t_employee` (
`eid` INT NOT NULL COMMENT '员工编号',
`ename` VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '员工姓名',
`salary` DOUBLE NOT NULL COMMENT '薪资',
`commission_pct` DECIMAL(3,2) DEFAULT NULL COMMENT '奖金比例',
`birthday` DATE NOT NULL COMMENT '出生日期',
`gender` ENUM('男','女') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '男' COMMENT '性别',
`tel` CHAR(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '手机号码',
`email` VARCHAR(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '邮箱',
`address` VARCHAR(150) DEFAULT NULL COMMENT '地址',
`work_place` SET('北京','深圳','上海','武汉') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '北京' COMMENT '工作地点'
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
//插入数据
INSERT INTO `t_employee`(`eid`,`ename`,`salary`,`commission_pct`,`birthday`,`gender`,`tel`,`email`,`address`,`work_place`)
VALUES (1,'孙洪亮',28000,'0.65','1980-10-08','男','13789098765','shl@atguigu.com','白庙村西街','北京,深圳'),
(2,'何进',7001,'0.10','1984-08-03','男','13456732145','hj@atguigu.com','半截塔存','深圳,上海'),
(3,'邓超远',8000,NULL,'1985-04-09','男','18678973456','dcy666@atguigu.com','宏福苑','北京,深圳,上海,武汉'),
(4,'黄熙萌',9456,NULL,'1986-09-07','女','13609876789','hxm@atguigu.com','白庙村东街','深圳,上海,武汉'),
(5,'陈浩',8567,NULL,'1978-08-02','男','13409876545','ch888@atguigu.com','回龙观','北京,深圳,上海'),
(6,'韩庚年',12000,NULL,'1985-04-03','男','18945678986','hgn@atguigu.com','龙泽','深圳,上海'),
(7,'贾宝玉',15700,'0.24','1982-08-02','男','15490876789','jby@atguigu.com','霍营','北京,武汉'),
(8,'李晨熙',9000,'0.40','1983-03-02','女','13587689098','lc@atguigu.com','东三旗','深圳,上海,武汉'),
(9,'李易峰',7897,NULL,'1984-09-01','男','13467676789','lyf@atguigu.com','西山旗','武汉'),
(10,'陆风',8789,NULL,'1989-04-02','男','13689876789','lf@atguigu.com','天通苑一区','北京'),
(11,'黄冰茹',15678,NULL,'1983-05-07','女','13787876565','hbr@atguigu.com','立水桥','深圳'),
(12,'孙红梅',9000,NULL,'1986-04-02','女','13576234554','shm@atguigu.com','立城苑','上海'),
(13,'李冰冰',18760,NULL,'1987-04-09','女','13790909887','lbb@atguigu.com','王府温馨公寓','北京'),
(14,'谢吉娜',18978,'0.25','1990-01-01','女','13234543245','xjn@atguigu.com','园中园','上海,武汉'),
(15,'董吉祥',8978,NULL,'1987-05-05','男','13876544333','djx@atguigu.com','小辛庄','北京,上海'),
(16,'彭超越',9878,NULL,'1988-03-06','男','18264578930','pcy@atguigu.com','西二旗','深圳,武汉'),
(17,'李诗雨',9000,NULL,'1990-08-09','女','18567899098','lsy@atguigu.com','清河','北京,深圳,武汉'),
(18,'舒淇格',16788,'0.10','1978-09-04','女','18654565634','sqg@atguigu.com','名流花园','北京,深圳,武汉'),
(19,'周旭飞',7876,NULL,'1988-06-13','女','13589893434','sxf@atguigu.com','小汤山','北京,深圳'),
(20,'章嘉怡',15099,'0.10','1989-12-11','女','15634238979','zjy@atguigu.com','望都家园','北京'),
(21,'白露',9787,NULL,'1989-09-04','女','18909876789','bl@atguigu.com','西湖新村','上海'),
(22,'刘烨',13099,'0.32','1990-11-09','男','18890980989','ly@atguigu.com','多彩公寓','北京,上海'),
(23,'陈纲',13090,NULL,'1990-02-04','男','18712345632','cg@atguigu.com','天通苑二区','深圳'),
(24,'吉日格勒',10289,NULL,'1990-04-01','男','17290876543','jrgl@163.com','北苑','北京'),
(25,'额日古那',9087,NULL,'1989-08-01','女','18709675645','ergn@atguigu.com','望京','北京,上海'),
(26,'李红',5000,NULL,'1995-02-15','女','15985759663','lihong@atguigu.com','冠雅苑','北京'),
(27,'周洲',8000,NULL,'1990-01-01','男','13574528569','zhouzhou@atguigu.com','冠华苑','北京,深圳');
查询全部员工信息
*代表全部信息都查出来
SELECT * FROM t_employee;
查询全部员工的名字和工资
select 列名1,列名2 from 表名
select ename,salary from t_employee
查询全部员工的名字和工资,并且把名字修改为姓名
select ename as 姓名,salary from t_employee
去重满足列重复的数据
去除满足列名1和列名2一样的,然后去重
select DISTINCT 列名1,列名2 from 表名
关键字 distinct 去除重复的
select DISTINCT salary,gender from t_employee
# 查询员工的性别种类!
select DISTINCT gender from t_employee
制造一个常量列
//意思就是查询的时候临时添加一个列,列名为aaa,这个列的数据都是'上谷歌'
select 列名1,列名2,列数据 列名 from t_employee
select ename as 姓名,salary,'上谷歌' aaa from t_employee
查询所有员工姓名,月薪和年薪(年薪等于月薪*12,结果列字段为 姓名 , 月薪 , 年薪 )
中间空一格或者用as 都是起别名的意思
select ename 姓名,salary 月薪,salary * 12 年薪 from t_employee
select ename 姓名,salary 月薪,salary * 12 年薪 from t_employee
# 4. 查询所有员工姓名,月薪和年薪(年薪等于月薪*12,结果列字段为 姓名 , 月薪 , 年薪 )
这个关键字的意思就是假如这列的数据某行为null ,那么就变成填的第二个值
ifnull(列名,变成的值)
select ename as 姓名,salary as 月薪,ifnull(commission_pct,0) * salary '月奖金',((ifnull(commission_pct,0)) * salary + salary) as 月总 from t_employee
# 6. 查询所有员工一共有几种薪资
distinct 去重的意思,只保留一个
这句话的意思就是去重这一列,重复的然后只保留一个
select distinct 列名 from 表名
select distinct salary from t_employee