數據庫概述
- 什么是數據庫: 數據庫就是一個文件系統(tǒng),只不過需要通過命令(sql)來操作這個文件系統(tǒng)
- 數據庫的作用:存儲數據,數據的倉局,帶有不同訪問權限的人可以有不同的操作
- 常見的關系型數據庫:mysql, oracle, mariadb(mysql分支,和mysql非常相似),db2, sqlserver
- 常見的非關系型數據庫:MongoDB, redis
- 關系型數據庫:主要描述實體與實體的關系
mysql的簡單使用
安裝和卸載
安裝:安裝教程
卸載:卸載mysqlServer,刪除mysql目錄下所有文件,刪除mysql所有存儲數據。
mysql的一些語句
DDL(定義): create drop alter(修改)
DML(操作): insert update delete
DCL(數據控制語句): 定義訪問權限,取消訪問權限,安全設置,grant
DQL(查詢):select from子句 where子句
常用的數據庫crud操作
創(chuàng)建數據庫
- 登錄
mysql -uroot -proot //(賬號密碼為root root)
- 創(chuàng)建一個數據庫DeeJay
create database DeeJay1;
創(chuàng)建數據庫的時候要指定字符集的話可以這么寫:
create database DeeJay1 character set utf8;
create database DeeJay1 character set utf8 collate 校對規(guī)則;
-
查看數據庫
- 查看所有數據庫
輸出如下:show databases;
可以看到還有一些默認庫。mysql> show databases; +--------------------+ | Database | +--------------------+ | deejay1 | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.02 sec)- 查看數據庫定義的語句:
show create database 數據庫名;mysql> show create database DeeJay1; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | DeeJay1 | CREATE DATABASE `DeeJay1` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+------------------------------------------------------------------+ 1 row in set (0.01 sec)
- 查看所有數據庫
-
修改數據庫的操作
- 修改數據庫的字符集
alter database 數據庫名 character set 字符集;mysql> alter database DeeJay1 character set gbk; Query OK, 1 row affected (0.13 sec) mysql> show create database DeeJay1; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | DeeJay1 | CREATE DATABASE `DeeJay1` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) 刪除數據庫
drop database 數據庫名;
mysql> drop database DeeJay1;
Query OK, 0 rows affected (0.19 sec)
mysql> show create database DeeJay1;
ERROR 1049 (42000): Unknown database 'deejay1'
- 其他數據庫操作
- 切換數據庫(選中數據庫)
use 數據庫名;- 查看當前正在使用的數據庫
select database();mysql> create database base1; Query OK, 1 row affected (0.15 sec) mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> use base1; Database changed mysql> select database(); +------------+ | database() | +------------+ | base1 | +------------+ 1 row in set (0.00 sec)
常用的表的crud操作
創(chuàng)建表
create table 表名(
列名 列的類型 約束,
列名2 列2的類型 約束
);
列的類型(和java做下對比):
| java | sql |
|---|---|
| int | int |
| char/String | char/varchar |
| double | double |
| float | float |
| boolean | boolean |
| date | date(YYYY-MM-DD)/time(hh:mm:ss)/datetime(YYYY-MM-DD hh:mm:ss 默認為null)/timestamp(YYYY-MM-DD hh:mm:ss 默認為當前時間) |
char: 固定長度 varchar:可變長度
數據庫中的列類型還有text(主要用來存儲文本)和blob(主要用來存放二進制)
列的約束:
主鍵約束: primary key
唯一約束: unique
非空約束: not null
創(chuàng)建表舉例:
- 分析實體: 學生
- 學生ID
- 姓名
- 性別
- 年齡
mysql> create table student(studentId int primary key, name varchar(25), gender boolean, age int);
Query OK, 0 rows affected (0.72 sec)
查看表
- 查看所有的表
show tables;
mysql> show tables;
+-----------------+
| Tables_in_base1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.07 sec)
- 查看表的定義
show create table 表名;
mysql> show create table student;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`studentId` int(11) NOT NULL,
`name` varchar(25) DEFAULT NULL,
`gender` tinyint(1) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`studentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)
- 查看表結構
desc 表名;
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| studentId | int(11) | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| gender | tinyint(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.04 sec)
修改表
alter table 表名 修改的類型 列名 列的類型 列的約束;
修改的類型: 添加列(add), 修改列(modify), 修改列名(change), 刪除列(drop), 修改表名(rename)
- 添加列(add)
mysql> alter table student add score int not null; // 給student添加一列成績,為int型,并且不能為null
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| studentId | int(11) | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| gender | tinyint(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | int(11) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
- 修改列(modify)
mysql> alter table student modify gender int;// 修改student的gender列的類型為int
Query OK, 0 rows affected (0.75 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| studentId | int(11) | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| gender | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | int(11) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
- 修改列名(change)
mysql> alter table student change name studentname varchar(25);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| studentId | int(11) | NO | PRI | NULL | |
| studentname | varchar(25) | YES | | NULL | |
| gender | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | int(11) | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
- 刪除列(drop)
mysql> alter table student drop score;
Query OK, 0 rows affected (0.68 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| studentId | int(11) | NO | PRI | NULL | |
| studentname | varchar(25) | YES | | NULL | |
| gender | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
- 修改表名(rename), 修改表的字符集
這兩種方法一般不常用,慎用。
rename table student to normalstudent;
alter table student character set gbk;
刪除表
drop table 表名;
mysql> drop table student;
Query OK, 0 rows affected (0.24 sec)
mysql> show tables;
Empty set (0.00 sec)
sql對表中數據的CRUD操作
現有如下結構的一個表:
mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| ismale | tinyint(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
插入數據
insert into 表名(列名1,列名2,列名3) values(值1, 值2, 值3);
mysql> insert into student(id, name, ismale, age) values(1, "zhangsan", true, 20);
Query OK, 1 row affected (0.08 sec)
mysql> select * from student;
+----+----------+--------+------+
| id | name | ismale | age |
+----+----------+--------+------+
| 1 | zhangsan | 1 | 20 |
+----+----------+--------+------+
1 row in set (0.00 sec)
對于上述的表中所有列都插入數據的情況,可以簡寫為:
insert into 表名 values(值1, 值2, 值3);
mysql> insert into student values(2, "lisi", true, 18);
Query OK, 1 row affected (0.08 sec)
mysql> select * from student;
+----+----------+--------+------+
| id | name | ismale | age |
+----+----------+--------+------+
| 1 | zhangsan | 1 | 20 |
| 2 | lisi | 1 | 18 |
+----+----------+--------+------+
2 rows in set (0.00 sec)
也可以靈活指定想插入的列。
批量插入:
insert into 表名 values(值1, 值2, 值3),(值1, 值2, 值3),(值1, 值2, 值3);
單條插入和批量插入的效率問題: 批量會比較快,但是如果某一條數據有問題的話會都失敗。
mysql> insert into student values(3, "Yang", true, 23),(4, "Wen", false, 25);
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+----------+--------+------+
| id | name | ismale | age |
+----+----------+--------+------+
| 1 | zhangsan | 1 | 20 |
| 2 | lisi | 1 | 18 |
| 3 | Yang | 1 | 23 |
| 4 | Wen | 0 | 25 |
+----+----------+--------+------+
4 rows in set (0.00 sec)
刪除記錄
delete from 表名 [where 條件]
mysql> delete from student where name='zhangsan';
Query OK, 1 row affected (0.06 sec)
mysql> select * from student;
+----+------+--------+------+
| id | name | ismale | age |
+----+------+--------+------+
| 2 | lisi | 1 | 18 |
| 3 | Yang | 1 | 23 |
| 4 | Wen | 0 | 25 |
+----+------+--------+------+
3 rows in set (0.00 sec)
delete from student;如果沒有where條件,執(zhí)行這個語句的話會將數據一條一條全部刪除。
delete刪除數據和truncate刪除數據的區(qū)別:delete屬于DML,是一條一條刪除表中數據, truncate屬于DDL,先去刪除表再去重建表。
如果數據較少那么delete較快,如果數據多,那么truncate較快。
修改表記錄
update 表名 set 列名=列的值,列名2=值2 [where 條件]
mysql> update student set name="zhangsan" where id=2; // 將id為2的那一列的name改為zhangsan
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+----------+--------+------+
| id | name | ismale | age |
+----+----------+--------+------+
| 2 | zhangsan | 1 | 18 |
| 3 | Yang | 1 | 23 |
| 4 | Wen | 0 | 25 |
+----+----------+--------+------+
3 rows in set (0.00 sec)
update student set name="zhangsan",ismale=false;如果后面不跟where條件,那么表中所有的列的name和ismale列都會改。
查詢記錄
select [distinct] [ * ] [列名1,列名2] from 表名 [where 條件];
distinct: 去除重復的數據 distinct詳細用法
首先創(chuàng)建一個商品分類的表:
create table category(
cid int primary key auto_increment,
cname varchar(25),
cdesc varchar(31)
);
然后插入數據:
insert into category values(null, "手機數碼", "電子產品");
insert into category values(null, "鞋靴箱包", "江南皮鞋廠打造");
insert into category values(null, "香煙酒水", "芙蓉王,茅臺");
insert into category values(null, "酸奶餅干", "哇哈哈");
insert into category values(null, "饞嘴零食", "瓜子花生");
現在這個表的結構為:
mysql> select * from category;
+-----+--------------+-----------------------+
| cid | cname | cdesc |
+-----+--------------+-----------------------+
| 1 | 手機數碼 | 電子產品 |
| 2 | 鞋靴箱包 | 江南皮鞋廠打造 |
| 3 | 香煙酒水 | 芙蓉王,茅臺 |
| 4 | 酸奶餅干 | 哇哈哈 |
| 5 | 饞嘴零食 | 瓜子花生 |
+-----+--------------+-----------------------+
5 rows in set (0.00 sec)
對于指定列的查詢:
mysql> select cname,cdesc from category;
+--------------+-----------------------+
| cname | cdesc |
+--------------+-----------------------+
| 手機數碼 | 電子產品 |
| 鞋靴箱包 | 江南皮鞋廠打造 |
| 香煙酒水 | 芙蓉王,茅臺 |
| 酸奶餅干 | 哇哈哈 |
| 饞嘴零食 | 瓜子花生 |
+--------------+-----------------------+
5 rows in set (0.00 sec)
再來創(chuàng)建一張商品的表,商品和商品分類的關系為所屬關系:
create table product(
pid int primary key auto_increment,
pname varchar(25),
price double, // 價格
pdate timestamp, // 生產日期
cno int // 商品分類的id
);
插入一些數據:
insert into product values(null, "小米mix2s", 2700, current_timestamp, 1);
insert into product values(null, "華為p30", 4788, current_timestamp, 1);
insert into product values(null, "阿迪王", 99, current_timestamp, 2);
insert into product values(null, "老村長", 88, current_timestamp, 3);
insert into product values(null, "勁酒", 35, current_timestamp, 3);
insert into product values(null, "小熊餅干", 3, current_timestamp, 4);
insert into product values(null, "衛(wèi)龍辣條", 1, current_timestamp, 5);
insert into product values(null, "旺旺雪餅", 2, current_timestamp, 5);
當前product表為:
mysql> select * from product;
+-----+--------------+-------+---------------------+------+
| pid | pname | price | pdate | cno |
+-----+--------------+-------+---------------------+------+
| 4 | 小米mix2s | 2700 | 2019-04-14 13:14:31 | 1 |
| 5 | 華為p30 | 4788 | 2019-04-14 13:14:32 | 1 |
| 6 | 阿迪王 | 99 | 2019-04-14 13:14:32 | 2 |
| 7 | 老村長 | 88 | 2019-04-14 13:14:32 | 3 |
| 8 | 勁酒 | 35 | 2019-04-14 13:14:32 | 3 |
| 9 | 小熊餅干 | 3 | 2019-04-14 13:14:32 | 4 |
| 10 | 衛(wèi)龍辣條 | 1 | 2019-04-14 13:14:32 | 5 |
| 11 | 旺旺雪餅 | 1 | 2019-04-14 13:14:32 | 5 |
+-----+--------------+-------+---------------------+------+
8 rows in set (0.00 sec)
-
簡單查詢
-
查詢所有商品:
select * from product; -
查詢商品名稱和價格
select pname,price from product; -
別名查詢.
as關鍵字, as可以省略- 表別名,
select p.pname, p.price from product as p(主要用于多表查詢)select p.pname, p.price from product as p; - 列別名,
select pname as 商品名稱, price as 商品價格 from product;
可以看到,輸出的列別名已經發(fā)生了更改。mysql> select pname as 商品名稱, price as 商品價格 from product; +--------------+--------------+ | 商品名稱 | 商品價格 | +--------------+--------------+ | 小米mix2s | 2700 | | 華為p30 | 4788 | | 阿迪王 | 99 | | 老村長 | 88 | | 勁酒 | 35 | | 小熊餅干 | 3 | | 衛(wèi)龍辣條 | 1 | | 旺旺雪餅 | 1 | +--------------+--------------+ 8 rows in set (0.00 sec)
- 表別名,
-
去掉重復的值
- 查詢商品所有的價格:
輸出的值有些是重復的,那么我們可以引入distinct來去掉重復的值:mysql> select price from product; +-------+ | price | +-------+ | 2700 | | 4788 | | 99 | | 88 | | 35 | | 3 | | 1 | | 1 | +-------+ 8 rows in set (0.00 sec)mysql> select distinct price from product; +-------+ | price | +-------+ | 2700 | | 4788 | | 99 | | 88 | | 35 | | 3 | | 1 | +-------+ 7 rows in set (0.00 sec)
- 查詢商品所有的價格:
-
select查詢運算
假設我們現在所有的商品都要打8折,那么可以這樣查詢輸出:
mysql> select *,price*0.8 from product; +-----+--------------+-------+---------------------+------+--------------------+ | pid | pname | price | pdate | cno | price*0.8 | +-----+--------------+-------+---------------------+------+--------------------+ | 4 | 小米mix2s | 2700 | 2019-04-14 13:14:31 | 1 | 2160 | | 5 | 華為p30 | 4788 | 2019-04-14 13:14:32 | 1 | 3830.4 | | 6 | 阿迪王 | 99 | 2019-04-14 13:14:32 | 2 | 79.2 | | 7 | 老村長 | 88 | 2019-04-14 13:14:32 | 3 | 70.4 | | 8 | 勁酒 | 35 | 2019-04-14 13:14:32 | 3 | 28 | | 9 | 小熊餅干 | 3 | 2019-04-14 13:14:32 | 4 | 2.4000000000000004 | | 10 | 衛(wèi)龍辣條 | 1 | 2019-04-14 13:14:32 | 5 | 0.8 | | 11 | 旺旺雪餅 | 1 | 2019-04-14 13:14:32 | 5 | 0.8 | +-----+--------------+-------+---------------------+------+--------------------+ 8 rows in set (0.00 sec)可以加一個列別名:
mysql> select *,price*0.8 as 8折折后價 from product; +-----+--------------+-------+---------------------+------+--------------------+ | pid | pname | price | pdate | cno | 8折折后價 | +-----+--------------+-------+---------------------+------+--------------------+ | 4 | 小米mix2s | 2700 | 2019-04-14 13:14:31 | 1 | 2160 | | 5 | 華為p30 | 4788 | 2019-04-14 13:14:32 | 1 | 3830.4 | | 6 | 阿迪王 | 99 | 2019-04-14 13:14:32 | 2 | 79.2 | | 7 | 老村長 | 88 | 2019-04-14 13:14:32 | 3 | 70.4 | | 8 | 勁酒 | 35 | 2019-04-14 13:14:32 | 3 | 28 | | 9 | 小熊餅干 | 3 | 2019-04-14 13:14:32 | 4 | 2.4000000000000004 | | 10 | 衛(wèi)龍辣條 | 1 | 2019-04-14 13:14:32 | 5 | 0.8 | | 11 | 旺旺雪餅 | 1 | 2019-04-14 13:14:32 | 5 | 0.8 | +-----+--------------+-------+---------------------+------+--------------------+ 8 rows in set (0.00 sec)上述的輸出,其實并不改變庫內的數據,僅僅是在查詢結果上做了運算。
-
where關鍵字進行條件查詢
指定條件,確定要查詢記錄
- 查詢價格大于60的商品:
select * from where price > 60; - where后的關系運算符:
> >= < <= = != <><>和!=都為不等于,區(qū)別在于<>是標準sql語法而!=是非標準的sql語法 - where后的邏輯運算符:
and, or, not,判斷某一列是否為空:is null,is not null - 查詢價格大于10并且小于100的商品:
select * from product where price < 100 and price > 10;
也可以使用between...and...,select * from product where price between 10 and 100; - like 模糊查詢
-
_代表的是一個字符 -
%代表的是多個字符 - 查詢出商品名稱中帶有"小"的所有商品:
select * from product where pname like "%小%";mysql> select * from product where pname like "%小%"; +-----+--------------+-------+---------------------+------+ | pid | pname | price | pdate | cno | +-----+--------------+-------+---------------------+------+ | 4 | 小米mix2s | 2700 | 2019-04-14 13:14:31 | 1 | | 9 | 小熊餅干 | 3 | 2019-04-14 13:14:32 | 4 | +-----+--------------+-------+---------------------+------+ 2 rows in set (0.00 sec) - 查詢名字第二個字是'熊'的商品:
select * from product where pname like "_熊%";mysql> select * from product where pname like "_熊%"; +-----+--------------+-------+---------------------+------+ | pid | pname | price | pdate | cno | +-----+--------------+-------+---------------------+------+ | 9 | 小熊餅干 | 3 | 2019-04-14 13:14:32 | 4 | +-----+--------------+-------+---------------------+------+ 1 row in set (0.00 sec)
-
- in 在某個范圍中獲得值
- 查詢出商品分類id在1,4,5里面的所有商品
select * from product where cno in (1,4,5);
- 查詢出商品分類id在1,4,5里面的所有商品
- 查詢價格大于60的商品:
-
排序查詢:order by 關鍵字. 關鍵字: asc(ascend升序,為默認值), desc(descend降序).
- 查詢所有商品進行按價格降序排列:
select * from product order by price desc; - 查詢商品名稱中含有"小"的所有商品并且進行按價格降序排列:
select * from product where pname like "%小%" order by price desc;
- 查詢所有商品進行按價格降序排列:
-
-
復雜查詢
- 聚合函數:
函數 | 作用
sum() | 求和
count() | 統(tǒng)計數量
max() | 最大值
min() | 最小值
avg() | 平均值- 獲得所有商品價格的總和:
select sum(price) from product; - 獲得所有商品價格的平均值:
select avg(price) from product; - 獲得所有商品的個數:
select count(*) from product;
需要注意的是:where后面不能接聚合函數!
由于where后面不能加聚合函數,所以如果想要查詢到價格大于平均價格的所有商品時。需要這么寫(子查詢):
select * from product where price > (select avg(price) from product);mysql> select * from product where price > (select avg(price) from product); +-----+-------------+-------+---------------------+------+ | pid | pname | price | pdate | cno | +-----+-------------+-------+---------------------+------+ | 4 | 小米mix2s | 2700 | 2019-04-14 13:14:31 | 1 | | 5 | 華為p30 | 4788 | 2019-04-14 13:14:32 | 1 | +-----+-------------+-------+---------------------+------+ 2 rows in set (0.00 sec)-
分組: group by
- 根據cno字段去進行分組,統(tǒng)計各個種類商品的個數:
select cno as 商品種類,count(*) as 商品個數 from product group by cno;mysql> select cno as 商品種類,count(*) as 商品個數 from product group by cno; +--------------+--------------+ | 商品種類 | 商品個數 | +--------------+--------------+ | 1 | 2 | | 2 | 1 | | 3 | 2 | | 4 | 1 | | 5 | 2 | +--------------+--------------+ 5 rows in set (0.00 sec)- 根據cno分組,分組統(tǒng)計每組商品的平均價格,并且商品平均價格>60
直接根據cno分組,分組統(tǒng)計每組商品的平均價格可以這樣寫:
select cno as 商品種類, avg(price) as 平均價格 from product group by cno;mysql> select cno as 商品種類, avg(price) as 平均價格 from product group by cno; +--------------+--------------+ | 商品種類 | 平均價格 | +--------------+--------------+ | 1 | 3744 | | 2 | 99 | | 3 | 61.5 | | 4 | 3 | | 5 | 1 | +--------------+--------------+ 5 rows in set (0.00 sec)還有過濾掉平均價格不大于60的商品,要使用
having關鍵字:select cno as 商品種類, avg(price) as 平均價格 from product group by cno having avg(price) > 60;mysql> select cno as 商品種類, avg(price) as 平均價格 from product group by cno having avg(price) > 60; +--------------+--------------+ | 商品種類 | 平均價格 | +--------------+--------------+ | 1 | 3744 | | 2 | 99 | | 3 | 61.5 | +--------------+--------------+ 3 rows in set (0.00 sec)having關鍵字,可以接聚合函數,并且出現在分組之后
where關鍵字,不可以接聚合函數,并且出現在分組之前
sql語句的編寫順序和執(zhí)行順序
- 編寫順序:
select ... from ... where ... group by ... having ... order by ... - 執(zhí)行順序:
from ... where ... group by ... having ... select ... order by ...