MySQL入门(参考官网)
一、登陆和退出
1. 远程主机
shell> mysql -h host -u user -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>
2. 登陆本机
shell> mysql -u user -p
3. 退出mysql
mysql> QUIT
二、输入查询
查询版本号和当前日期
mysql> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 5.7.24 | 2018-11-12 |
+-----------+--------------+
1 row in set (0.01 sec)
mysql>
大小写不敏感
把mysql当计算器用
mysql> SELECT SIN(PI()/4), (4+1)*5;
+--------------------+---------+
| SIN(PI()/4) | (4+1)*5 |
+--------------------+---------+
| 0.7071067811865476 | 25 |
+--------------------+---------+
1 row in set (0.01 sec)
在一行上输入多个语句。分号结束每一个命令:
mysql> SELECT VERSION(); SELECT NOW();
+-----------+
| VERSION() |
+-----------+
| 5.7.24 |
+-----------+
1 row in set (0.00 sec)
+---------------------+
| NOW() |
+---------------------+
| 2018-11-13 00:10:14 |
+---------------------+
1 row in set (0.00 sec)
mysql 通过 查找终止分号;
来确定语句的结束位置
mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
+----------------+--------------+
| USER() | CURRENT_DATE |
+----------------+--------------+
| root@localhost | 2018-11-13 |
+----------------+--------------+
1 row in set (0.00 sec)
mysql> SELECT USER(),CURRENT_DATE;
+----------------+--------------+
| USER() | CURRENT_DATE |
+----------------+--------------+
| root@localhost | 2018-11-13 |
+----------------+--------------+
1 row in set (0.00 sec)
通过键入以下内容取消该查询:\c
mysql> SELECT
-> USER()
-> \c
三、创建和使用数据库
展示存在的数据库
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| falcon |
| mysql |
| performance_schema |
| sys |
+--------------------+
使用数据库
mysql> USE test
Database changed
shell> mysql -h host -u user -p menagerie
Enter password: ********
3.1 创建和选择数据库
mysql> CREATE DATABASE menagerie;
mysql> USE menagerie
Database changed
3.2 创建表
mysql> SHOW TABLES;
Empty set (0.00 sec)
创建表
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
展示表单:
mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| pet |
+---------------------+
1 row in set (0.00 sec)
描述表单
mysql> 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 | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
3.3 将数据加载到表中
建立表格以后需要:
name | owner | species | sex | birth | death |
---|---|---|---|---|---|
Fluffy | Harold | cat | f | 1993-02-04 | |
Claws | Gwen | cat | m | 1994-03-17 | |
Buffy | Harold | dog | f | 1989-05-13 | |
Fang | Benny | dog | m | 1990-08-27 | |
Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
Chirpy | Gwen | bird | f | 1998-09-11 | |
Whistler | Gwen | bird | 1997-12-09 | ||
Slim | Benny | snake | m | 1996-04-29 |
保存为pet.txt
从excel中直接复制到pet.txt,空值用\N 表示
Whistler Gwen bird \N 1997-12-09 \N
3.3.1 导入数据
LOAD DATA LOCAL INFILE 'D:/Temp/pet.txt' INTO TABLE pet;
win编辑器
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet
-> LINES TERMINATED BY '\r\n';
在运行 os x 的 apple 计算机上, 您可能想要使用。LINES TERMINATED BY '\r'
3.3.2 插入语句
mysql> INSERT INTO pet
-> VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
3.4 操作表格
SELECT 语法:
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
3.4.1 查询所有数据
mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| 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 | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+
update 语句修复错误记录 :
mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';
3.4.2 查询某行
选择特定行:
mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
选择特定条件:
mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Chirpy | Gwen | bird | f | 1998-09-11 | 0000-00-00 |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+-------+---------+------+------------+------------+
条件and:
mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | 0000-00-00 |
+-------+--------+---------+------+------------+------------+
条件or:
mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Chirpy | Gwen | bird | f | 1998-09-11 | 0000-00-00 |
| Whistler | Gwen | bird | | 1997-12-09 | 0000-00-00 |
| Slim | Benny | snake | m | 1996-04-29 | 0000-00-00 |
+----------+-------+---------+------+------------+------------+
and 、 or 混合使用:
mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
-> OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | 0000-00-00 |
| Buffy | Harold | dog | f | 1989-05-13 | 0000-00-00 |
+-------+--------+---------+------+------------+------------+
3.4.3 查询某列
选择某列,按照 ,分隔
mysql> SELECT name`,` birth FROM pet;
+----------+------------+
| name | birth |
+----------+------------+
| name | 0000-00-00 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1990-08-27 |
| Bowser | 1989-08-31 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Puffball | 1999-03-30 |
+----------+------------+
选择动物主人:
mysql> SELECT owner FROM pet;
+--------+
| owner |
+--------+
| owner |
| Harold |
| Gwen |
| Harold |
| Benny |
| Diane |
| Gwen |
| Gwen |
| Benny |
| Diane |
+--------+
使用DISTINCT
去重
mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner |
+--------+
| owner |
| Harold |
| Gwen |
| Benny |
| Diane |
+--------+
使用WHERE
条件查询
mysql> SELECT name, species, birth FROM pet
-> WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+
| name | species | birth |
+--------+---------+------------+
| Fluffy | cat | 1993-02-04 |
| Claws | cat | 1994-03-17 |
| Buffy | dog | 1989-05-13 |
| Fang | dog | 1990-08-27 |
| Bowser | dog | 1989-08-31 |
+--------+---------+------------+
3.4.4 排序行
使用ORDER BY
,默认升序
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| name | 0000-00-00 |
| Buffy | 1989-05-13 |
| Bowser | 1989-08-31 |
| Fang | 1990-08-27 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+
默认不区分大小写排序,如果要区分使用:
ORDER BY BINARY col_name
BINARY:转换为二进制字符,方便排序
使用ORDER BY
,默认升序,如果降序,添加DESC
mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
+----------+------------+
| name | birth |
+----------+------------+
| Puffball | 1999-03-30 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Claws | 1994-03-17 |
| Fluffy | 1993-02-04 |
| Fang | 1990-08-27 |
| Bowser | 1989-08-31 |
| Buffy | 1989-05-13 |
| name | 0000-00-00 |
+----------+------------+
多个列排序:
先按照动物种类升序,然后按照出生日期降序
mysql> 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 |
| Fang | dog | 1990-08-27 |
| Bowser | dog | 1989-08-31 |
| Buffy | dog | 1989-05-13 |
| Puffball | hamster | 1999-03-30 |
| Slim | snake | 1996-04-29 |
| name | species | 0000-00-00 |
+----------+---------+------------+
3.4.5 日期计算
TIMESTAMPDIFF():时间差函数
mysql> SELECT TIMESTAMPDIFF(YEAR,'1989-08-31',CURDATE());
+--------------------------------------------+
| TIMESTAMPDIFF(YEAR,'1989-08-31',CURDATE()) |
+--------------------------------------------+
| 29 |
+--------------------------------------------+
常用时间函数:
mysql> SELECT NOW(),CURDATE(),CURTIME();
+---------------------+------------+-----------+
| NOW() | CURDATE() | CURTIME() |
+---------------------+------------+-----------+
| 2018-11-13 10:21:28 | 2018-11-13 | 10:21:28 |
+---------------------+------------+-----------+
查询显示每个宠物的出生日期、当前日期,用age做别名
mysql> SELECT name, birth, CURDATE(),
-> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
-> FROM pet;
+----------+------------+------------+------+
| name | birth | CURDATE() | age |
+----------+------------+------------+------+
| Fluffy | 1993-02-04 | 2018-11-13 | 25 |
| Claws | 1994-03-17 | 2018-11-13 | 24 |
| Buffy | 1989-05-13 | 2018-11-13 | 29 |
| Fang | 1990-08-27 | 2018-11-13 | 28 |
| Bowser | 1989-08-31 | 2018-11-13 | 29 |
| Chirpy | 1998-09-11 | 2018-11-13 | 20 |
| Whistler | 1997-12-09 | 2018-11-13 | 20 |
| Slim | 1996-04-29 | 2018-11-13 | 22 |
| Puffball | 1999-03-30 | 2018-11-13 | 19 |
+----------+------------+------------+------+
查询同时 按照姓名排序
mysql> SELECT name, birth, CURDATE(),
-> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
-> FROM pet ORDER BY name;
+----------+------------+------------+------+
| name | birth | CURDATE() | age |
+----------+------------+------------+------+
| Bowser | 1989-08-31 | 2018-11-13 | 29 |
| Buffy | 1989-05-13 | 2018-11-13 | 29 |
| Chirpy | 1998-09-11 | 2018-11-13 | 20 |
| Claws | 1994-03-17 | 2018-11-13 | 24 |
| Fang | 1990-08-27 | 2018-11-13 | 28 |
| Fluffy | 1993-02-04 | 2018-11-13 | 25 |
| Puffball | 1999-03-30 | 2018-11-13 | 19 |
| Slim | 1996-04-29 | 2018-11-13 | 22 |
| Whistler | 1997-12-09 | 2018-11-13 | 20 |
+----------+------------+------------+------+
查询同时 按照年龄排序
mysql> SELECT name, birth, CURDATE(),
-> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
-> FROM pet ORDER BY age;
+----------+------------+------------+------+
| name | birth | CURDATE() | age |
+----------+------------+------------+------+
| Puffball | 1999-03-30 | 2018-11-13 | 19 |
| Chirpy | 1998-09-11 | 2018-11-13 | 20 |
| Whistler | 1997-12-09 | 2018-11-13 | 20 |
| Slim | 1996-04-29 | 2018-11-13 | 22 |
| Claws | 1994-03-17 | 2018-11-13 | 24 |
| Fluffy | 1993-02-04 | 2018-11-13 | 25 |
| Fang | 1990-08-27 | 2018-11-13 | 28 |
| Buffy | 1989-05-13 | 2018-11-13 | 29 |
| Bowser | 1989-08-31 | 2018-11-13 | 29 |
+----------+------------+------------+------+
查询已经死亡的宠物
把默认导入的数值从'0000-00-00' 改成'NUll'
UPDATE pet SET death = NULL WHERE death= 0000-00-00;
mysql> SELECT name, birth, death,
-> TIMESTAMPDIFF(YEAR,birth,death) AS age
-> FROM pet WHERE death IS NOT NULL ORDER BY age;
+--------+------------+------------+------+
| name | birth | death | age |
+--------+------------+------------+------+
| Bowser | 1989-08-31 | 1995-07-29 | 5 |
+--------+------------+------------+------+
MySQL 的一些日期函数:
YEAR():返回年
MONTH():返回月
DAYOFMONTH():返回日
DATE_ADD() :添加时间
mysql> SELECT YEAR('1987-01-01');
+--------------------+
| YEAR('1987-01-01') |
+--------------------+
| 1987 |
+--------------------+
mysql> SELECT MONTH('2008-02-03');
+---------------------+
| MONTH('2008-02-03') |
+---------------------+
| 2 |
+---------------------+
mysql> SELECT MONTHNAME('2008-02-03');
+-------------------------+
| MONTHNAME('2008-02-03') |
+-------------------------+
| February |
+-------------------------+
现在是今年的 多少天 多少周
mysql> SELECT DAYOFYEAR(now()),WEEKOFYEAR(now());
+------------------+-------------------+
| DAYOFYEAR(now()) | WEEKOFYEAR(now()) |
+------------------+-------------------+
| 317 | 46 |
+------------------+-------------------+
2008-01-03第多少天,多少周
mysql> SELECT DAYOFYEAR('2008-01-03'),WEEKOFYEAR('2008-01-03');
+-------------------------+--------------------------+
| DAYOFYEAR('2008-01-03') | WEEKOFYEAR('2008-01-03') |
+-------------------------+--------------------------+
| 3 | 1 |
+-------------------------+--------------------------+
查询宠物出生月
mysql> SELECT name, birth, MONTH(birth) FROM pet;
+----------+------------+--------------+
| name | birth | MONTH(birth) |
+----------+------------+--------------+
| Fluffy | 1993-02-04 | 2 |
| Claws | 1994-03-17 | 3 |
| Buffy | 1989-05-13 | 5 |
| Fang | 1990-08-27 | 8 |
| Bowser | 1989-08-31 | 8 |
| Chirpy | 1998-09-11 | 9 |
| Whistler | 1997-12-09 | 12 |
| Slim | 1996-04-29 | 4 |
| Puffball | 1999-03-30 | 3 |
+----------+------------+--------------+
查询5月出生的宠物
mysql> SELECT name, birth FROM pet WHERE MONTH(birth) = 5;
+-------+------------+
| name | birth |
+-------+------------+
| Buffy | 1989-05-13 |
+-------+------------+
2018-05-01 添加1天
mysql> SELECT DATE_ADD('2018-05-01',INTERVAL 1 DAY);
+---------------------------------------+
| DATE_ADD('2018-05-01',INTERVAL 1 DAY) |
+---------------------------------------+
| 2018-05-02 |
+---------------------------------------+
查询下个月是哪个月
mysql> SELECT MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
+---------------------------------------------+
| MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH)) |
+---------------------------------------------+
| 12 |
+---------------------------------------------+
查询即将生日的宠物,距离目前最近的一个月
- 方法1:当前月份+1个月
mysql> SELECT name, birth FROM pet
-> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
+----------+------------+
| name | birth |
+----------+------------+
| Whistler | 1997-12-09 |
+----------+------------+
- 方法2:当前月份取余+1
mysql> SELECT name, birth FROM pet
-> WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
+----------+------------+
| name | birth |
+----------+------------+
| Whistler | 1997-12-09 |
+----------+------------+
- MOD(12, 5)
mysql> SELECT MOD(12, 5);
+------------+
| MOD(12, 5) |
+------------+
| 2 |
+------------+
3.4.6 使用空值
NULL value :a missing unknown value,要和其他值区别对待
测试NULL,使用
mysql> SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
| 0 | 1 |
+-----------+---------------+
mysql> SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;
+-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
+-----------+---------------+------------+----------------+
| 0 | 1 | 0 | 1 |
+-----------+---------------+------------+----------------+
不能使用算术笔记用算符判断
null
=, <, or <>
3.4.7 匹配方式
- 标准的 sql 模式匹配
- 正则表达式的模式匹配
- 默认不区分大小写
标准匹配
使用:
查找b开头的名字:
mysql> SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
查找fy结尾的名字:
mysql> SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
查找名字包含一个w
:
mysql> SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+
查找名字是5个字符的:
mysql> SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
正则匹配
使用:
RLIKE
NOT RLIKE
和上述等价
查找b开头的名字:
mysql> SELECT * FROM pet WHERE name REGEXP '^b';
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
若要强制regexp
比较区分大小写, 请使用binary
关键字将其中一个字符串设置为二进制字符串。
匹配名称由小写:b
开头
SELECT * FROM pet WHERE name REGEXP BINARY '^b';
查找fy结尾的名字:
mysql> SELECT * FROM pet WHERE name REGEXP 'fy$';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
查找名字包含一个w
:
mysql> SELECT * FROM pet WHERE name REGEXP 'w';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+
查找名字是5个字符的
mysql> SELECT * FROM pet WHERE name REGEXP '^.....$';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
或者^.{5}$
:^
开头$
结尾
mysql> SELECT * FROM pet WHERE name REGEXP '^.{5}$';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
3.4.8 统计行
COUNT(*) 统计函数
统计行数
mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 9 |
+----------+
统计每个人有多少只宠物
mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner | COUNT(*) |
+--------+----------+
| Benny | 2 |
| Diane | 2 |
| Gwen | 3 |
| Harold | 2 |
+--------+----------+
统计每种动物的数量:
mysql> SELECT species, COUNT(*) FROM pet GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| bird | 2 |
| cat | 2 |
| dog | 3 |
| hamster | 1 |
| snake | 1 |
统计物种性别:
mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex;
+------+----------+
| sex | COUNT(*) |
+------+----------+
| NULL | 1 |
| f | 4 |
| m | 4 |
+------+----------+
物种和性别的组合:
mysql> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| bird | NULL | 1 |
| bird | f | 1 |
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| dog | m | 2 |
| hamster | f | 1 |
| snake | m | 1 |
+---------+------+----------+
只针对某个字段筛选,不用筛选整行
选择cat 和 dog种类,按照性别物种组合
mysql> SELECT species, sex, COUNT(*) FROM pet
-> WHERE species = 'dog' OR species = 'cat'
-> GROUP BY species, sex;
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| dog | m | 2 |
+---------+------+----------+
列出已知动物性别:
mysql> SELECT species, sex, COUNT(*) FROM pet
-> WHERE sex IS NOT NULL
-> GROUP BY species, sex;
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| bird | f | 1 |
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| dog | m | 2 |
| hamster | f | 1 |
| snake | m | 1 |
+---------+------+----------+
mysql> SET sql_mode = 'ONLY_FULL_GROUP_BY';-- 启用
mysql> SELECT owner, COUNT(*) FROM pet;
ERROR 1140 (42000):
ONLY_FULL_GROUP_BY 关闭后,结果不确定,系统随意添加一个值,最好关闭
mysql> SET sql_mode = ''; -- 关闭
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT owner, COUNT(*) FROM pet;
+--------+----------+
| owner | COUNT(*) |
+--------+----------+
| Harold | 9 |
+--------+----------+
mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner | COUNT(*) |
+--------+----------+
| Benny | 2 |
| Diane | 2 |
| Gwen | 3 |
| Harold | 2 |
+--------+----------+
3.4.9 使用多个表
此表格跟踪记录你的宠物,当你想记录他们的信息,比如看兽医、生幼崽、买玩具等事件,就需要另外一个表单记录,需要如下信息:
- 宠物名称:name
- 事件日期:date
- 事件类型:type
- 事件描述:emark
创建表一个符合上述要素的表单:
mysql> CREATE TABLE event (name VARCHAR(20), date DATE,
-> type VARCHAR(15), remark VARCHAR(255));
将下述内容存储为event.txt
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 | |
Fang | 1991-10-12 | kennel | |
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 |
导入内容到表单
mysql> LOAD DATA LOCAL INFILE 'event.txt' INTO TABLE event;
假设:想知道每个宠物下崽时的年龄,宠物妈妈下崽时间在event
表单,但是,出生日期在pet
表单,所以要同时查询2个表单。
mysql> SELECT pet.name,
-> TIMESTAMPDIFF(YEAR,birth,date) AS age,
-> remark
-> FROM pet INNER JOIN event
-> ON pet.name = event.name
-> WHERE event.type = 'litter';
+--------+------+------------------------------+
| name | age | remark |
+--------+------+------------------------------+
|Fluffy | 2 | 4 kittens, 3 female, 1 male
|Buffy | 4 | 5 puppies, 2 female, 3 male
| 5 | 3 puppies, 3 female
+--------+------+------------------------------+
jion表单:
- 是为从两张表获取信息
- 需要指定匹配规则,上述例子都有'name'
- 需要指定前缀
ON pet.name = event.name
表的自身连接
宠物的雌雄配对:
mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species
-> FROM pet AS p1 INNER JOIN pet AS p2
-> ON p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';
+--------+------+--------+------+---------+
| name | sex | name | sex | species |
+--------+------+--------+------+---------+
| Fluffy | f | Claws | m | cat |
| Buffy | f | Fang | m | dog |
| Buffy | f | Bowser | m | dog |
+--------+------+--------+------+---------+
3.4 获取数据库和表的信息
查看目前使用的数据库
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| menagerie |
+------------+
查看表
mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| event |
| pet |
+---------------------+
查看表结构
mysql> 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 | |
+---------+-------------+------+-----+---------+-------+
3.5 批处理模式
在mysql内运行脚本:
mysql> source filename;
mysql> \. filename
来源:MySQL官网
MySQL入门(参考官网)的更多相关文章
- MySql.Data.dll官网下载
Mysql.Data.dll官网下载 在项目开发中链接MySQL数据库经常要用到Mysql.Data.dll,网上虽然有很多,但是还是比较信赖官网的 今天就从官网下载一次记录一下过程 1.下载地址 官 ...
- github--hello,world(参考官网)
官网:https://guides.github.com/activities/hello-world/ 一共分为5步. 1.为你的项目新建仓库(repository): 2.新建分支(branch) ...
- Jmeter入门1 官网下载及安装
1 Jmeter运行需要java环境.首先需要安装JDK. 图标是这样的: 2 下载apache-jmeter包. jmeter官网:http://jmeter.apache.org/ 3 解压 ...
- mysql的优化:官网地址
http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#index-merge-optimization
- centos7 mysql 5.7 官网下载tar安装
https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下载好上传到服务器,解压后以此安装 libs,client,server三个rpm r ...
- mysql官网下载对应的mysql包
1. 在百度搜索mysql,点击mysql官网上下载mysql的地址 在url直接输入mysql的下载地址也可以:https://dev.mysql.com/downloads/mysql/ 如图: ...
- 【Spark深入学习 -16】官网学习SparkSQL
----本节内容-------1.概览 1.1 Spark SQL 1.2 DatSets和DataFrame2.动手干活 2.1 契入点:SparkSess ...
- Bootstrap--模仿官网写一个页面
本文参考Bootstrap官方文档写了简单页面来熟悉Bootstrap的栅格系统.常用CSS样.Javascript插件和部分组件. 以下html代码可以直接复制本地运行: BootstrapPage ...
- MySQL入门——在Linux下安装和卸载MySQL
MySQL入门——在Linux下安装和卸载MySQL 摘要:本文主要学习了如何在Linux系统中安装和卸载MySQL数据库. 查看有没有安装过MySQL 使用命令查看有没有安装过: [root@loc ...
随机推荐
- Linux安装python2.7
一.Python安装 以Python-2.7.7为例,安装包:Python-2.7.7.tgz如无特殊说明,以下安装步骤都采用root用户执行 1. 解压Python-2.7.7.tgz tar -x ...
- gulp配置(编译压缩转码自动刷新注释全)
参考自:http://www.sheyilin.com/2016/02/gulp_introduce/ 在原先基础上增加了less编译 es6转码资源地图等,修改了一部分的热刷新. gulpfile. ...
- 【Git】+ 新建+删除+上传+覆盖
上传代码时邮箱格式不符合:https://blog.csdn.net/u012558695/article/details/64921922 在本地新建一个分支: git branch newBran ...
- [Unity优化]批处理04:MaterialPropertyBlock
参考链接: https://blog.csdn.net/liweizhao/article/details/81937590 1.在场景中放一些Cube,赋予一个新材质,使用内置shader(Unli ...
- 时序数据库InfluxDB:简介及安装
在性能测试过程中,对测试结果以及的实时监控与展示也是很重要的一部分.这篇博客,介绍下linux环境下InfluxDB的安装以及功能特点. 官网地址:influxdata 官方文档:influxdb文档 ...
- SkylineGlobe 7.0.1 & 7.0.2版本Web开发 如何实现对三维模型和地形的剖切展示
现在很多三维项目中,不仅仅要用到三维地形,正射影像和矢量数据,还会融合到各种三维模型,包括传统的3DMax手工建模,BIM,倾斜摄影自动建模,激光点云模型,三维地质体模型等等. 三维平台首先要做的是把 ...
- HDU-problem-1002-人类史上最大最好的希望事件-矩阵快速幂
Problem Description 作为CNCS的半壁江山,狗哥常常在宇宙中心邵阳眺望黄浦江,夜晚的星空总是迷人,有时候还能见到彗星滑落. 狗哥是幸运的,他在两秒钟内看到了十七颗彗星划过天际,作为 ...
- 【原创】微服务为什么一定要用docker
引言 早在2013年的时候,docker就已经发行,然而那会还是很少人了解docker.一直到2014年,Martin Fowler提出了微服务的概念,两个不相干的技术终于走在了一起,创造了今天的辉煌 ...
- js 运行机制
<script> console.log(1) setTimeout(function(){ console.log(3) },0) console.log(2) </script& ...
- 阿里云 轻量应用服务器(LAMP) 使用日志记录
phpStudy(PHP运行环境一键安装包) https://www.jb51.net/softs/182860.html 0:PHP开发工具 https://netbeans.org/downloa ...