8單錶練習2

2021-10-24 18:26:05 字數 4143 閱讀 4615

use j2005_db;

set foreign_key_checks = off;

drop table if exists department;

create table department

(`id`        int  auto_increment comment '部門id',

`name`      varchar(15)         comment '部門名稱',

`location`  varchar(13)         comment '部門所在地',

primary key (id)

) engine = innodb

comment '部門資訊表'; 

drop table if exists employee;

create table employee

(`id`            int auto_increment comment '員工id',

`name`          varchar(16)        comment '員工姓名',

`job`           varchar(16)        comment '員工崗位名稱',

`manager_id`    int                comment '員工上級領導編號',

`hire_date`     date               comment '入職日期',

`salary`        int                comment '工資',

`commission`    int                comment '佣金/獎金',

`department_id` int                comment '所屬部門編號',

primary key (id)

) engine = innodb

comment '雇員資訊表'; 

# alter table employee drop foreign key fk_employee_department_id

alter table employee 

add constraint fk_employee_department_id 

foreign key employee(department_id) 

references department(id);

insert into department values 

(1, 'accounting', 'new york'),

(2, 'research', 'dallas'),

(3, 'sales', 'chicago'),

(4, 'operations', 'boston');

insert into employee values 

(1, 'smith', 'clerk', 13, '1980-12-17', 800, null, 2),

(2, 'allen', 'salesman', 6, '1981-02-20', 1600, 300, 3),

(3, 'ward', 'salesman', 6, '1981-02-22', 1250, 500, 3),

(4, 'jones', 'manager', 9, '1981-04-02', 2975, null, 2),

(5, 'martin', 'salesman', 6, '1981-09-28', 1250, 1400, 3),

(6, 'blake', 'manager', 9, '1981-05-01', 2850, null, 3),

(7, 'clark', 'manager', 9, '1981-06-09', 2450, null, 1),

(8, 'scott', 'analyst', 4, '1987-07-13', 3000, null, 2),

(9, 'king', 'president', null, '1981-11-17', 5000, null, 1),

(10, 'turner', 'salesman', 6, '1981-09-08', 1500, null, 3),

(11, 'adams', 'clerk', 8, '1987-07-13', 1100, null, 2),

(12, 'james', 'clerk', 6, '1981-12-03', 950, null, 3),

(13, 'ford', 'analyst', 4, '1981-12-03', 3000, null, 2),

(14, 'miller', 'clerk', 7, '1982-01-23', 1300, null, 1);

set foreign_key_checks = on;

--   --------------------------------

檢視兩張表的資訊

select * from department;

select * from employee;

-- ------------------------------------

-- 1. 查詢所有員工資訊

select * from employee;

-- 2. 查詢指定的字段,只查詢姓名和工資

select name,salary from employee;

-- 3. 查詢smith的所有資訊

select * from employee where name='smith';

-- 4. 查詢工資在800-1500之間的員工所有資訊

select * from employee where salary between 800 and 1500;

select * from employee where salary >= 800 and salary <= 1500;

-- 5. 統計一共有多種職業

select count(distinct job)  多種職業 from employee;

-- 6. 使用 as 給表或者列取別名

-- 7. 求出所有員工的總工資(總工資=月薪+獎金)

select salary+ifnull(commission,0) 總工資 from employee 

-- ifnull(引數1,引數2) 若引數1為null 使用引數2代替

-- 8. 查詢出前5條員工的資訊

select * from employee where id<=5 order by id;

select * from employee order by id limit 0,5;

-- 9. 查詢出有獎金的員工的姓名、職位、工資

select name,job,salary from employee where commission is not  null; 

-- 10. where 子句,設定查詢條件

--     101- 查詢工資大於1000的員工資訊

select * from employee where salary>1000;

--     102- 查詢不是salesman的員工資訊

select * from employee where job !='salesman';

--     103- 查詢not between...and查詢工資不在 1000-2000之間的員工資訊

select * from employee where salary  not between 1000 and 2000;

--     104- 模糊查詢,查詢出最後1個字母為s的員工資訊

select * from employee where name  like '%s';

--     105- and > or > not

--          and 所有條件都必須滿足  or 只有有1個條件滿足  not 取反

-- 11. 按照員工的工資進行降序排序,再按照入職時間進行公升序排序

select * from employee order by salary desc,hire_date asc;

-- 12. 按照部門編號進行分組,統計每個部門的員工人數

select  department_id 部門編號,count(*) 員工人數  from employee group by department_id 

MySQL 2 單錶查詢(練習1)

mysql uroot p密碼 show databases 檢視所有資料庫 use db1 使用其中乙個資料庫create table dept deptno int primary key,dname varchar 14 部門名稱 loc varchar 13 部門位址 insert into...

單錶查詢練習題

create table employee id int,name varchar 20 password varchar 20 gender varchar 10 age int default 25,email varchar 50 salary double 8,2 state int,dep...

MySQL之單錶查詢練習

一 emp表 二 練習 1.查詢出部門編號為30的所有員工 2.所有銷售員的姓名 編號和部門編號。3.找出獎金高於工資的員工。4.找出獎金高於工資60 的員工。5.找出部門編號為10中所有經理,和部門編號為20中所有銷售員的詳細資料。6.找出部門編號為10中所有經理,部門編號為20中所有銷售員,還有...