1 简单查询
select * from employee;
select name,salary from employee;
2 where条件
select name,salary from employee where salary > 10000;
select name,salary from employee where salary > 10000 and salary < 20000;
select name,salary from employee where salary between 10000 and 20000;
select name,salary from employee where salary not between 10000 and 20000;
select name,salary from employee where salary = 10000 or salary = 20000 or salary = 30000;
select name,salary from employee where salary in (10000,20000,30000);
select * from employee where salary = 10000 or age = 18 or sex='male';
select * from employee where post_comment is Null;
select * from employee where post_comment = Null;
select * from employee where post_comment is not Null;
select * from employee where name like '%n%';
select * from employee where name like 'e__n';
3 group by分组
mysql> select depart_id,group_concat(name) from employee group by depart_id;
+-----------+--------------------------------------------------------------+
| depart_id | group_concat(name) |
+-----------+--------------------------------------------------------------+
| 1 | egon,alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 |
| 2 | 歪歪,丫丫,丁丁,星星,格格 |
| 3 | 张野,程咬金,程咬银,程咬铜,程咬铁 |
+-----------+--------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql> select depart_id,count(id) from employee group by depart_id;
+-----------+-----------+
| depart_id | count(id) |
+-----------+-----------+
| 1 | 8 |
| 2 | 5 |
| 3 | 5 |
+-----------+-----------+
3 rows in set (0.01 sec)
mysql> select depart_id,group_concat(id) from employee group by depart_id;
+-----------+------------------+
| depart_id | group_concat(id) |
+-----------+------------------+
| 1 | 1,2,3,4,5,6,7,8 |
| 2 | 9,10,11,12,13 |
| 3 | 14,15,16,17,18 |
+-----------+------------------+
3 rows in set (0.00 sec)
mysql> select depart_id,count(id) from employee group by depart_id;
+-----------+-----------+
| depart_id | count(id) |
+-----------+-----------+
| 1 | 8 |
| 2 | 5 |
| 3 | 5 |
+-----------+-----------+
3 rows in set (0.00 sec)
mysql> select depart_id,max(salary) from employee group by depart_id;
+-----------+-------------+
| depart_id | max(salary) |
+-----------+-------------+
| 1 | 1000000.31 |
| 2 | 4000.33 |
| 3 | 20000.00 |
+-----------+-------------+
3 rows in set (0.00 sec)
mysql> select depart_id,min(salary) from employee group by depart_id;
+-----------+-------------+
| depart_id | min(salary) |
+-----------+-------------+
| 1 | 2100.00 |
| 2 | 1000.37 |
| 3 | 10000.13 |
+-----------+-------------+
3 rows in set (0.00 sec)
mysql> select depart_id,sum(salary) from employee group by depart_id;
+-----------+-------------+
| depart_id | sum(salary) |
+-----------+-------------+
| 1 | 1070200.64 |
| 2 | 13001.47 |
| 3 | 84000.13 |
+-----------+-------------+
3 rows in set (0.00 sec)
mysql> select depart_id,avg(salary) from employee group by depart_id;
+-----------+---------------+
| depart_id | avg(salary) |
+-----------+---------------+
| 1 | 133775.080000 |
| 2 | 2600.294000 |
| 3 | 16800.026000 |
+-----------+---------------+
3 rows in set (0.00 sec)
create table class(
cid int primary key auto_increment,
caption varchar(20)
);
create table student(
sid int primary key auto_increment,
sname varchar(20),
gender varchar(20),
class_id int,
foreign key(class_id) references class(cid)
on delete cascade
on update cascade
)engine=innodb;
create table teacher(
tid int primary key auto_increment,
tname varchar(20)
);
create table course(
cid int primary key auto_increment,
cname varchar(10),
teacher_id int,
foreign key(teacher_id) references teacher(tid)
on delete cascade
on update cascade
)engine=innodb;
create table score(
sid int primary key auto_increment,
student_id int not null,
corse_id int not null,
number int
);
insert into class(caption) values('三年二班'),('一年三班'),('三年一班');
insert into student(sname,gender,class_id) values('钢蛋','女',1),
('铁锤','女',1),
('山炮','男',2);
insert into teacher(tname) values('刘老师'),('田老师'),('林老师');
insert into course(cname,teacher_id) values('生物',1),('体育',1),('物理',2);
insert into score values(1,1,1,60),(2,1,2,59),(3,2,2,100);
练习:账号信息表,用户组,主机表,主机组
复制代码
#用户表
create table user(
id int not null unique auto_increment,
username varchar(20) not null,
password varchar(50) not null,
primary key(username,password)
);
insert into user(username,password) values
('root','123'),
('egon','456'),
('alex','alex3714')
;
#用户组表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20) not null unique
);
insert into usergroup(groupname) values
('IT'),
('Sale'),
('Finance'),
('boss')
;
#主机表
create table host(
id int primary key auto_increment,
ip char(15) not null unique default '127.0.0.1'
);
insert into host(ip) values
('172.16.45.2'),
('172.16.31.10'),
('172.16.45.3'),
('172.16.31.11'),
('172.10.45.3'),
('172.10.45.4'),
('172.10.45.5'),
('192.168.1.20'),
('192.168.1.21'),
('192.168.1.22'),
('192.168.2.23'),
('192.168.2.223'),
('192.168.2.24'),
('192.168.3.22'),
('192.168.3.23'),
('192.168.3.24')
;
#业务线表
create table business(
id int primary key auto_increment,
business varchar(20) not null unique
);
insert into business(business) values
('轻松贷'),
('随便花'),
('大富翁'),
('穷一生')
;
#建关系:user与usergroup
create table user2usergroup(
id int not null unique auto_increment,
user_id int not null,
group_id int not null,
primary key(user_id,group_id),
foreign key(user_id) references user(id),
foreign key(group_id) references usergroup(id)
);
insert into user2usergroup(user_id,group_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(2,3),
(2,4),
(3,4)
;
#建关系:host与business
create table host2business(
id int not null unique auto_increment,
host_id int not null,
business_id int not null,
primary key(host_id,business_id),
foreign key(host_id) references host(id),
foreign key(business_id) references business(id)
);
insert into host2business(host_id,business_id) values
(1,1),
(1,2),
(1,3),
(2,2),
(2,3),
(3,4)
;
#建关系:user与host
create table user2host(
id int not null unique auto_increment,
user_id int not null,
host_id int not null,
primary key(user_id,host_id),
foreign key(user_id) references user(id),
foreign key(host_id) references host(id)
);
insert into user2host(user_id,host_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(1,7),
(1,8),
(1,9),
(1,10),
(1,11),
(1,12),
(1,13),
(1,14),
(1,15),
(1,16),
(2,2),
(2,3),
(2,4),
(2,5),
(3,10),
(3,11),
(3,12)
employee 表
create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
);
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','male',18,'19000301','teacher',30000,401,1),
('成龙','male',48,'20101111','teacher',10000,401,1),
('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),
('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3)
;