创建数据库
CREATE DATABASE DATABASE_NAME;
create database pet;
选择数据库
USE DATABASE_NAME;
use pet;
查看所有数据库
SHOW DATABASES;
在当前数据库中创建表
CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
显示表中列的属性
DESCRIBE TABLE_NAME;
DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
DESCRIBE可以简写为desc
查看当前数据库内的所有表
SHOW TABLES;
+---------------+
| Tables_in_pet |
+---------------+
| pet |
+---------------+
将数据插入表
insert into pet values('Fluffy','Harold','cat','F','1993-02-04',NULL);
insert into pet values('Chirpy','Gwen','bird','F','1998-09-11',NULL);
insert into pet values('Whistler','Gwen','bird',NULL,'1997-12-09',NULL);
insert into pet values('Slim','Benny','snake','m','1996-04-29',NULL);
insert into pet values('Claws','Gwen','cat','m','1994-03-17',NULL);
insert into pet values('Buffy','Harold','dog','F','1989-05-13',NULL);
insert into pet values('Fang','Benny','dog','m','1990-08-27',NULL);
insert into pet values('Bowser','Diane','dog','F','1979-08-31','1995-07-29');
向表中插入数据,宠物名,主人,种类,性别,出生日期,死亡日期
查看表中所有数据
select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Chirpy | Gwen | bird | F | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | F | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | F | 1979-08-31 | 1995-07-29 |
+----------+--------+---------+------+------------+------------+
查看宠物与出生日期
select name,birth from pet;
+----------+------------+
| name | birth |
+----------+------------+
| Fluffy | 1993-02-04 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1990-08-27 |
| Bowser | 1979-08-31 |
+----------+------------+
查看特定行,例如查看名为Fluffy的宠物
select * from pet where name = 'fluffy';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
+--------+--------+---------+------+------------+-------+
查看宠物claws的生日
select name,birth from pet where name = 'claws';
+-------+------------+
| name | birth |
+-------+------------+
| Claws | 1994-03-17 |
+-------+------------+
查看所有生日大于1990-01-01的宠物与其主人
select * from pet where birth > '1990-01-01';
+----------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Chirpy | Gwen | bird | F | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
+----------+--------+---------+------+------------+-------+
可以利用and运算符,来定位母猫(物种为猫同时性别为母)
select * from pet where species = 'cat' and sex = 'f';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
+--------+--------+---------+------+------------+-------+
可以利用or运算符定位定位种类为狗与种类为猫的行
select * from pet where species = 'cat' or species = 'dog';
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | F | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | F | 1979-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
还可以将and与or运算符结合到一起使用,例如查找所有的母猫与公狗
select * from pet where (species = 'dog' and sex = 'm') or (species = 'cat' and sex = 'f');
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | F | 1993-02-04 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
+--------+--------+---------+------+------------+-------+
可以根据宠物的生日进行排序
升序
select name,species,birth from pet order by birth asc;
+----------+---------+------------+
| name | species | birth |
+----------+---------+------------+
| Bowser | dog | 1979-08-31 |
| Buffy | dog | 1989-05-13 |
| Fang | dog | 1990-08-27 |
| Fluffy | cat | 1993-02-04 |
| Fluffy | cat | 1993-02-04 |
| Claws | cat | 1994-03-17 |
| Slim | snake | 1996-04-29 |
| Whistler | bird | 1997-12-09 |
| Chirpy | bird | 1998-09-11 |
+----------+---------+------------+
降序
select name,species,birth from pet order by birth desc;
+----------+---------+------------+
| name | species | birth |
+----------+---------+------------+
| Chirpy | bird | 1998-09-11 |
| Whistler | bird | 1997-12-09 |
| Slim | snake | 1996-04-29 |
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |
| Fluffy | cat | 1993-02-04 |
| Fang | dog | 1990-08-27 |
| Buffy | dog | 1989-05-13 |
| Bowser | dog | 1979-08-31 |
+----------+---------+------------+
还可以将物种归类后在进行生日排序
select name,species,birth from pet order by species,birth asc;
+----------+---------+------------+
| name | species | birth |
+----------+---------+------------+
| Whistler | bird | 1997-12-09 |
| Chirpy | bird | 1998-09-11 |
| Fluffy | cat | 1993-02-04 |
| Fluffy | cat | 1993-02-04 |
| Claws | cat | 1994-03-17 |
| Bowser | dog | 1979-08-31 |
| Buffy | dog | 1989-05-13 |
| Fang | dog | 1990-08-27 |
| Slim | snake | 1996-04-29 |
+----------+---------+------------+
select name,species,birth from pet order by species,birth desc;
+----------+---------+------------+
| name | species | birth |
+----------+---------+------------+
| Chirpy | bird | 1998-09-11 |
| Whistler | bird | 1997-12-09 |
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |
| Fluffy | cat | 1993-02-04 |
| Fang | dog | 1990-08-27 |
| Buffy | dog | 1989-05-13 |
| Bowser | dog | 1979-08-31 |
| Slim | snake | 1996-04-29 |
+----------+---------+------------+
可以使用timestampdiff()函数来计算日期差,从而计算出年龄,详细用法请百度
SELECT petname, birth, CURDATE(), TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age FROM pet;
+----------+------------+------------+------+
| petname | birth | CURDATE() | age |
+----------+------------+------------+------+
| wangcai | 2016-03-04 | 2020-03-14 | 4 |
| dongdong | 2016-05-03 | 2020-03-14 | 3 |
| tangyuan | 2015-02-09 | 2020-03-14 | 5 |
| ergou | 2019-09-09 | 2020-03-14 | 0 |
| caiwang | 2016-05-03 | 2020-03-14 | 3 |
| lanqiu | 2019-06-09 | 2020-03-14 | 0 |
+----------+------------+------------+------+
可以使用timestampdiff()函数来列出所有宠物的生日所在的月
select petname,birth,month(birth) from pet;
+----------+------------+--------------+
| petname | birth | month(birth) |
+----------+------------+--------------+
| wangcai | 2016-03-04 | 3 |
| dongdong | 2016-05-03 | 5 |
| tangyuan | 2015-02-09 | 2 |
| ergou | 2019-09-09 | 9 |
| caiwang | 2016-05-03 | 5 |
| lanqiu | 2019-06-09 | 6 |
+----------+------------+--------------+
也可以列出所有生日在5月的宠物
select petname,birth,month(birth) from pet where month(birth) = '5';
+----------+------------+--------------+
| petname | birth | month(birth) |
+----------+------------+--------------+
| dongdong | 2016-05-03 | 5 |
| caiwang | 2016-05-03 | 5 |
+----------+------------+--------------+
%表示任意个数字符
_表示任意单个字符
查找所有以l(小写L)开头的宠物主人
select * from pet where owner like 'l%';
+----------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+----------+----------+---------+--------+------------+-------+
| liqiang | wangcai | gou | f | 2016-03-04 | NULL |
| liuqiang | dongdong | gou | m | 2016-05-03 | NULL |
| liyue | tangyuan | mao | f | 2015-02-09 | NULL |
| liqiang | caiwang | mao | m | 2016-05-03 | NULL |
+----------+----------+---------+--------+------------+-------+
查找所有以ng结尾的宠物主人
select * from pet where owner like '%ng';
+----------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+----------+----------+---------+--------+------------+-------+
| liqiang | wangcai | gou | f | 2016-03-04 | NULL |
| liuqiang | dongdong | gou | m | 2016-05-03 | NULL |
| liqiang | caiwang | mao | m | 2016-05-03 | NULL |
+----------+----------+---------+--------+------------+-------+
查找所有姓名中有i的宠物主人
select * from pet where owner like '%i%';
+----------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+----------+----------+---------+--------+------------+-------+
| liqiang | wangcai | gou | f | 2016-03-04 | NULL |
| liuqiang | dongdong | gou | m | 2016-05-03 | NULL |
| liyue | tangyuan | mao | f | 2015-02-09 | NULL |
| liqiang | caiwang | mao | m | 2016-05-03 | NULL |
+----------+----------+---------+--------+------------+-------+
查找姓名为5个字的宠物主人
select * from pet where owner like '_____';
+-------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+-------+----------+---------+--------+------------+-------+
| liyue | tangyuan | mao | f | 2015-02-09 | NULL |
+-------+----------+---------+--------+------------+-------+
REGEXP()函数
^表示为起始符
$表示为结尾符
.匹配任意单个字符
.{x}代表x个任意字符
查找所有名字l开头的宠物主人
select * from pet where regexp_like(owner,'^l');
+----------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+----------+----------+---------+--------+------------+-------+
| liqiang | wangcai | gou | f | 2016-03-04 | NULL |
| liuqiang | dongdong | gou | m | 2016-05-03 | NULL |
| liyue | tangyuan | mao | f | 2015-02-09 | NULL |
| liqiang | caiwang | mao | m | 2016-05-03 | NULL |
+----------+----------+---------+--------+------------+-------+
默认情况下,MySQL并不区分大小写,所以可以通过一些选项来区分大小写
SELECT * FROM pet WHERE REGEXP_LIKE(owner, '^l', 'c');
SELECT * FROM pet WHERE REGEXP_LIKE(owner, BINARY '^b');
SELECT * FROM pet WHERE REGEXP_LIKE(owner, '^l' COLLATE utf8mb4_0900_as_cs);
查找所有姓名以g结尾的宠物主
select * from pet where regexp_like(owner,'g$');
+----------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+----------+----------+---------+--------+------------+-------+
| liqiang | wangcai | gou | f | 2016-03-04 | NULL |
| liuqiang | dongdong | gou | m | 2016-05-03 | NULL |
| liqiang | caiwang | mao | m | 2016-05-03 | NULL |
+----------+----------+---------+--------+------------+-------+
查找所有姓名为5个字的宠物主
select * from pet where regexp_like(owner,'^.....$');
+-------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+-------+----------+---------+--------+------------+-------+
| liyue | tangyuan | mao | f | 2015-02-09 | NULL |
+-------+----------+---------+--------+------------+-------+
select * from pet where regexp_like(owner,'^.{5}$');
+-------+----------+---------+--------+------------+-------+
| owner | petname | species | gender | birth | death |
+-------+----------+---------+--------+------------+-------+
| liyue | tangyuan | mao | f | 2015-02-09 | NULL |
+-------+----------+---------+--------+------------+-------+
统计pet表中的行数量
select count(*) from pet;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
统计每个主人有多少个宠物
select owner,count(*) from pet group by owner;
+--------+----------+
| owner | count(*) |
+--------+----------+
| Harold | 2 |
| Gwen | 3 |
| Benny | 2 |
| Diane | 1 |
+--------+----------+
统计每种动物的数量
select species,count(*) from pet group by species;
+---------+----------+
| species | count(*) |
+---------+----------+
| bird | 2 |
| snake | 1 |
| cat | 2 |
| dog | 3 |
+---------+----------+
每种性别数量
select sex,count(*) from pet group by sex;
+------+----------+
| sex | count(*) |
+------+----------+
| F | 4 |
| NULL | 1 |
| m | 3 |
+------+----------+
每个物种与性别组合的数量
select species,sex,count(*) from pet group by species,sex;;
+---------+------+----------+
| species | sex | count(*) |
+---------+------+----------+
| bird | F | 1 |
| bird | NULL | 1 |
| snake | m | 1 |
| cat | m | 1 |
| dog | F | 2 |
| dog | m | 1 |
| cat | F | 1 |
+---------+------+----------+
统计猫狗各种性别的数量
select species,sex,count(*) from pet where species = 'dog' or species = 'cat' group by species,sex;
+---------+------+----------+
| species | sex | count(*) |
+---------+------+----------+
| cat | m | 1 |
| dog | F | 2 |
| dog | m | 1 |
| cat | F | 1 |
+---------+------+----------+
只统计已知性别的动物数量
select species,sex,count(*) from pet where sex is not null group by species,sex;
+---------+------+----------+
| species | sex | count(*) |
+---------+------+----------+
| bird | F | 1 |
| snake | m | 1 |
| cat | m | 1 |
| dog | F | 2 |
| dog | m | 1 |
| cat | F | 1 |
+---------+------+----------+
创建新表并插入数据
CREATE TABLE event (name VARCHAR(20), date DATE,type VARCHAR(15), remark VARCHAR(255));
insert into event values('Fluffy','1995-05-15','litter','4 kittens, 3 female, 1 male');
insert into event values('Buffy','1993-06-23','litter','5 puppies, 2 female, 3 male');
insert into event values('Buffy','1994-06-19','litter','3 puppies, 3 female');
insert into event values('Chirpy','1999-03-21','vet','needed beak straightened');
insert into event values('Slim','1997-08-03','vet','broken rib');
insert into event values('Bowser','1991-10-12','kennel',NULL);
insert into event values('Fang','1991-10-12',NULL);
insert into event values('Fang','1998-08-28','birthday','Gave him a new chew toy');
insert into event values('Claws','1998-03-17','birthday','Gave him a new flea collar');
insert into event values('Whistler','1998-12-09','birthday','First birthday');
查看表event
select * from event;
+----------+------------+----------+-----------------------------+
| name | date | type | remark |
+----------+------------+----------+-----------------------------+
| Fluffy | 1995-05-15 | litter | 4 kittens, 3 female, 1 male |
| Buffy | 1993-06-23 | litter | 5 puppies, 2 female, 3 male |
| Buffy | 1994-06-19 | litter | 3 puppies, 3 female |
| Chirpy | 1999-03-21 | vet | needed beak straightened |
| Slim | 1997-08-03 | vet | broken rib |
| Bowser | 1991-10-12 | kennel | NULL |
| Fang | 1991-10-12 | kennel | NULL |
| Fang | 1998-08-28 | birthday | Gave him a new chew toy |
| Claws | 1998-03-17 | birthday | Gave him a new flea collar |
| Whistler | 1998-12-09 | birthday | First birthday |
+----------+------------+----------+-----------------------------+
使用多表查询来获取所有产仔的宠物姓名,及remark
select pet.name,timestampdiff(year,pet.birth,event.date) as age,remark from pet inner join event on pet.name = event.name where event.type = 'litter';
+--------+------+-----------------------------+
| name | age | remark |
+--------+------+-----------------------------+
| Buffy | 5 | 3 puppies, 3 female |
| Buffy | 4 | 5 puppies, 2 female, 3 male |
| Fluffy | 2 | 4 kittens, 3 female, 1 male |
+--------+------+-----------------------------+
timestampdiff(year,pet.birth,event.date) as age:将pet.birth的日期作为起始时间,event.date的日期作为结束日期,以年为单位计算差,将结果列入age列中
pet inner join event:将pet表与event表做内链接
on pet.name = event.name:两表间的链接条件,要求pet.name的值与event.name的值相等才匹配
where event.type = 'littler':查找event.type列为litter
关于左链接,右链接,内链接的详细讲解在这里