mysql> create database if not exists mydb2 character set utf8;java
mysql> create table employee(mysql
-> id int,sql
-> name varchar(20),數據庫
-> sex varchar(20),express
-> birthday date,c#
-> entry_date date,oracle
-> job varchar(20),函數
-> salsay float(5,1),sqlserver
-> resume text);spa
Query OK, 0 rows affected (1.05 sec)
Insert
語法L
INSERT INTO table[(column[, column...])]
VALUES (value[,value...]);
l 插入的數據應與字段的數據類型相同。
l 數據的大小應在列的規定範圍內,例如:不能將一個長度爲80的字符串加入到長度爲40的列中。
l 在values中列出的數據位置必須與被加入的列的排列位置相對應。
l 字符和日期型數據應包含在單引號中。
l 插入空值,不指定或insert into table value(null)
l 注意:字符和日期要包含在單引號中。
mysql> insert into employee values(1,'java','male','1990-04-23','1990-05-08','pm
',5000.1,'hello');
Query OK, 1 row affected (0.11 sec)
mysql> insert into employee values(2,'mysql','male','1990-04-23','1990-05-08','pm',5000.1,'hello');
Query OK, 1 row affected(0.07 sec)
mysql> select *from employee;
+------+-------+------+------------+------------+------+--------+--------+
| id | name | sex | birthday | entry_date | job | salsay | resume|
+------+-------+------+------------+------------+------+--------+--------+
| 1 | java | male | 1990-04-23| 1990-05-08 | pm | 5000.1 | hello |
| 2 | mysql | male | 1990-04-23 | 1990-05-08| pm | 5000.1 | hello |
+------+-------+------+------------+------------+------+--------+--------+
2 rows in set (0.00 sec)
--刪除
語法:
delete from tbl_name
[WHEREwhere_definition]
l 若是不使用where子句,將刪除表中全部數據。
l delete語句不能刪除某一列的值(可以使用update)
l 使用delete語句僅刪除記錄,不刪除表自己。如要刪除表,使用drop table語句。用於刪除數據量不大的數據
l 同insert和update同樣,從一個表中刪除記錄將引發其它表的參照完整性問題,在修改數據庫數據時,
l 刪除表中數據也可以使用TRUNCATE TABLE語句,它和delete有所不一樣。TRUNCATE(複製表結構->一次性刪除整表->自動恢復表結構,適合刪除數據量較大的數據,不能按條件進行刪除
l DELETE(逐行刪除記錄),按行刪除表的記錄,但會保留表,適合刪除數據量不大的數據,可按條件刪除
drop table:刪除表的自己
刪除記錄時,必定要留意表間的關聯關係
mysql> deletefrom employee where id=1;
Query OK, 1 row affected (0.10 sec)
mysql> select * from employee;
+------+-------+------+------------+------------+------+--------+--------+
| id | name | sex | birthday | entry_date | job | salsay |resume |
+------+-------+------+------------+------------+------+--------+--------+
| 2 | mysql | male | 1990-04-23 | 1990-05-08| pm | 5000.1 | hello |
+------+-------+------+------------+------------+------+--------+--------+
1 row in set (0.00 sec)
--更新
l 使用 update語句修改表中數據。
語法:
UPDATE tbl_name
SETcol_name1=expr1[, col_name2=expr2 ...]
[WHEREwhere_definition]
l UPDATE語法能夠用新值更新原有錶行中的各列。
l SET子句指示要修改哪些列和要給予哪些值。
l WHERE子句指定應更新哪些行。如沒有WHERE子句,則更新全部的行
--先再插入幾條數據
mysql> insert into employee values(3,'c#','female','1991-04-23','1995-05-08','pm',6000.1,'how');
Query OK, 1 row affected (0.09 sec)
mysql> insert into employee values(4,'oracle','male','1950-04-23','1980-05-08','pm',1000.1,'are');
Query OK, 1 row affected (0.13 sec)
mysql> insert into employee values(5,'sqlserver','female','1970-04-23','1980-05-08','crm',4000.1,'you');
Query OK, 1 row affected (0.19 sec)
--將所人員的工資修改成5000
mysql> select * from employee;
+------+-----------+--------+------------+------------+------+--------+--------+
| id | name | sex | birthday | entry_date | job | salsay |resume |
+------+-----------+--------+------------+------------+------+--------+--------+
| 2 | mysql | male | 1990-04-23| 1990-05-08 | pm | 5000.1 | hello |
| 3 | c# | female | 1991-04-23| 1995-05-08 | pm | 6000.1 | how |
| 4 | oracle | male | 1950-04-23| 1980-05-08 | pm | 1000.1 | are |
| 5 | sqlserver | female | 1970-04-23| 1980-05-08 | crm | 4000.1 | you |
+------+-----------+--------+------------+------------+------+--------+--------+
4 rows in set (0.00 sec)
mysql> update employee set salsay=5000;
Query OK, 4 rows affected (0.05 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from employee;
+------+-----------+--------+------------+------------+------+--------+--------+
| id | name | sex | birthday | entry_date | job | salsay | resume|
+------+-----------+--------+------------+------------+------+--------+--------+
| 2 | mysql | male | 1990-04-23| 1990-05-08 | pm | 5000.0 | hello |
| 3 | c# | female | 1991-04-23| 1995-05-08 | pm | 5000.0 | how |
| 4| oracle | male | 1950-04-23| 1980-05-08 | pm | 5000.0 | are |
| 5 | sqlserver | female | 1970-04-23| 1980-05-08 | crm | 5000.0 | you |
+------+-----------+--------+------------+------------+------+--------+--------+
4 rows in set (0.00 sec)
--修改id爲2的員工的工資爲8000
mysql> update employee set salsay=8000 where id=2;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from employee where id=2;
+------+-------+------+------------+------------+------+--------+--------+
| id | name | sex | birthday | entry_date | job | salsay |resume |
+------+-------+------+------------+------------+------+--------+--------+
| 2 | mysql | male | 1990-04-23 | 1990-05-08| pm | 8000.0 | hello |
+------+-------+------+------------+------------+------+--------+--------+
1 row in set (0.00 sec)
=========================select=======================
mysql> create table student(
-> id int,
-> name varchar(20),
-> chinese float,
-> english float,
-> math float
-> );
Query OK, 0 rows affected (1.37 sec)
mysql>
mysql> insert into student(id,name,chinese,english,math) values(1,'張小明',89,78,90);
Query OK, 1 row affected (0.12 sec)
mysql> insert into student(id,name,chinese,english,math) values(2,'李進',67,98,56);
Query OK, 1 row affected (0.06 sec)
mysql> insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
Query OK, 1 row affected (0.37 sec)
mysql> insert into student(id,name,chinese,english,math) values(4,'李一',88,98,90);
Query OK, 1 row affected (0.06 sec)
mysql> insert into student(id,name,chinese,english,math) values(5,'李來財',82,84,67);
Query OK, 1 row affected (0.04 sec)
mysql> insert into student(id,name,chinese,english,math) values(6,'張進寶',55,85,45);
Query OK, 1 row affected (0.09 sec)
mysql> insert into student(id,name,chinese,english,math) values(7,'黃蓉',75,65,30);
Query OK, 1 row affected (0.05 sec)
mysql> insert into student(id,name,chinese,english,math) values(8,'張一李',75,65,30);
Query OK, 1 row affected (0.25 sec)
mysql> insert into student(id,name,chinese,english,math) values(9,'何李',75,65,30);
Query OK, 1 row affected (0.06 sec)
mysql> insert into student(id,name,chinese,english,math) values(10,'單',75,65,30);
Query OK, 1 row affected (0.14 sec)
mysql> insert into student(id,name,chinese,english,math) values(11,'李',75,65,NULL);
Query OK, 1 row affected (0.11 sec)
mysql> insert into student(id,name,chinese,english,math) values(12,'jack',75,65,40);
Query OK, 1 row affected (0.08 sec)
mysql> insert into student(id,name,chinese,english,math)values(13,'marry',75,65,60);
Query OK, 1 row affected (0.12 sec)
mysql>
mysql> select * from student;
+------+-----------+---------+---------+------+
| id | name | chinese | english |math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
| 2 | 李進 | 67 | 98 | 56 |
| 3 | 王五 | 87 | 78 | 77 |
| 4 | 李一 | 88 | 98 | 90 |
| 5 | 李來財 | 82 | 84 | 67 |
| 6 | 張進寶 | 55 | 85 | 45 |
| 7 | 黃蓉 | 75 | 65 | 30 |
| 8 | 張一李 | 75 | 65 | 30 |
| 9 | 何李 | 75 | 65 | 30 |
| 10 | 單 | 75 | 65 | 30 |
| 11 | 李 | 75 | 65 | NULL |
| 12 | jack | 75 | 65 | 40 |
| 13 | marry | 75 | 65 | 60 |
+------+-----------+---------+---------+------+
13 rows in set (0.00 sec)
l 基本select語句
語法:
SELECT [DISTINCT] *|{column1,column2. column3..}
FROM table;
l Select指定查詢哪些列的數據。
l column指定列名。
l *號表明查詢全部列。
l From指定查詢哪張表。
l DISTINCT可選,指顯示結果時,是否剔除重複數據
查詢表中全部學生的信息
select * from student;
select id,name,match,chinese,english from student;
select name,id,match,english,chinese from student;
--查詢表中全部學生的姓名和英語成績
mysql> select name,english from student;
+-----------+---------+
| name | english |
+-----------+---------+
| 張小明 | 78 |
| 李進 | 98 |
| 王五 | 78 |
| 李一 | 98 |
| 李來財 | 84 |
| 張進寶 | 85 |
| 黃蓉 | 65 |
| 張一李 | 65 |
| 何李 | 65 |
| 單 | 65 |
| 李 | 65 |
| jack | 65 |
| marry | 65 |
+-----------+---------+
13 rows in set(0.00 sec)
--select distinct/*/列名 from表名
--使用distinct過濾重複數據
mysql> select distinct english from student;
+---------+
| english |
+---------+
| 78 |
| 98 |
| 84 |
| 85 |
| 65 |
+---------+
5 rows in set(0.00 sec)
-- select 表達式/對列名加別名 from 表名 NULL+X=NULL
語法:
SELECT *|{column1|expression,column2|expression,..}
FROM table;
--在全部學生數學分數上加10分的特長分
--SELECT column as別名 from 表名;使用as別名
mysql> select name,math+10 as 數學 from student;
+-----------+--------+
| name | 數學 |
+-----------+--------+
| 張小明 | 100 |
| 李進 | 66 |
| 王五 | 87 |
| 李一 | 100 |
| 李來財 | 77 |
| 張進寶 | 55 |
| 黃蓉 | 40 |
| 張一李 | 40 |
| 何李 | 40 |
| 單 | 40 |
| 李 | NULL |
| jack | 50 |
| marry | 70 |
+-----------+--------+
13 rows in set(0.00 sec)| marry | 75 |
+-----------+------------+
13 rows in set(0.00 sec)
--統計每一個學生的總分:
mysql> select name,math+english+chinese as 總分 from student;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 張小明 | 257 |
| 李進 | 221 |
| 王五 | 242 |
| 李一 | 276 |
| 李來財 | 233 |
| 張進寶 | 185 |
| 黃蓉 | 170 |
| 張一李 | 170 |
| 何李 | 170 |
| 單 | 170 |
| 李 | NULL |
| jack | 180 |
| marry | 200 |
+-----------+--------+
13 rows in set(0.00 sec)
--where子句,出如今from後面,where是按行篩選
比較運算符 |
> < <= >= = <> |
大於、小於、大於(小於)等於、不等於 |
BETWEEN ...AND... |
顯示在某一區間的值 |
|
IN(set) |
顯示在in列表中的值,例:in(100,200) |
|
LIKE ‘張pattern’ |
模糊查詢(重點) |
|
IS NULL/IS NOT NULL |
判斷是否爲空 |
|
邏輯運算符 |
and && |
多個條件同時成立 |
or || |
多個條件任一成立 |
|
not ! |
不成立,例:where not(salary>100); |
Like語句中,%表明零個或多個任意字符,_表明一個字符,例first_name like ‘_a%’;
--查詢姓名爲張小明的記錄
mysql> select * from student where name='張小明';
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
+------+-----------+---------+---------+------+
1 row in set(0.00 sec)
--查詢英語大於90的學生
mysql> select * from student where english>90;
+------+--------+---------+---------+------+
| id | name | chinese | english | math |
+------+--------+---------+---------+------+
| 2 | 李進 | 67 | 98 | 56 |
| 4 | 李一 | 88 | 98 | 90 |
+------+--------+---------+---------+------+
2 rows in set(0.00 sec)
--查詢總分大於200的
mysql> select name, (chinese+english+math) as總分from student where (chinese+english+math)>200;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 張小明 | 257 |
| 李進 | 221 |
| 王五 | 242 |
| 李一 | 276 |
| 李來財 | 233 |
+-----------+--------+
5 rows in set(0.00 sec)
--查詢英語分數在80-90之間的同窗。
mysql> select *
-> from student
-> where english>=80 and english<=90;
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 5 | 李來財 | 82 | 84 | 67 |
| 6 | 張進寶 | 55 | 85 | 45 |
+------+-----------+---------+---------+------+
2 rows in set(0.00 sec)
或 (推薦下面的方式)
mysql> select *
-> from student
-> where english between 80 and 90;
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 5 | 李來財 | 82 | 84 | 67 |
| 6 | 張進寶 | 55 | 85 | 45 |
+------+-----------+---------+---------+------+
2 rows in set (0.00 sec)
--查詢數學分數爲89,90,91的同窗。
--方式一
mysql> select *
-> from student
-> where math=89 or math= 90 or math=91;
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
| 4 | 李一 | 88 | 98 | 90 |
+------+-----------+---------+---------+------+
2 rows in set(0.00 sec)
--方式二
mysql> select * from student where math in(89,90,91);
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
| 4 | 李一 | 88 | 98 | 90 |
+------+-----------+---------+---------+------+
2 rows in set(0.00 sec)
--查詢數學分數不爲89,90,91的同窗。
mysql> select * from student where math not in(89,90,91);
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 2 | 李進 | 67 | 98 | 56 |
| 3 | 王五 | 87 | 78 | 77 |
| 5 | 李來財 | 82 | 84 | 67 |
| 6 | 張進寶 | 55| 85 | 45 |
| 7 | 黃蓉 | 75 | 65 | 30 |
| 8 | 張一李 | 75 | 65 | 30 |
| 9 | 何李 | 75 | 65 | 30 |
| 10 | 單 | 75 | 65 | 30 |
| 12 | jack | 75 | 65 | 40 |
| 13 | marry | 75 | 65 | 60 |
+------+-----------+---------+---------+------+
10 rows in set(0.00 sec)
--查詢全部姓'李'的學生
mysql> select * from student where name like '李%';
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 2 | 李進 | 67 | 98 | 56 |
| 4 | 李一 | 88 | 98 | 90 |
| 5 | 李來財 | 82 | 84 | 67 |
| 11 | 李 | 75 | 65 | NULL |
+------+-----------+---------+---------+------+
4 rows in set(0.00 sec)
--查詢李_(_表明一個字符,%表明多個字符)
mysql> select * from student where name like '李_';
+------+--------+---------+---------+------+
| id | name | chinese | english | math |
+------+--------+---------+---------+------+
| 2 | 李進 | 67 | 98 | 56 |
| 4 | 李一 | 88 | 98 | 90 |
+------+--------+---------+---------+------+
2 rows in set(0.00 sec)
--模糊查詢,查詢有姓名中有'李'的字
mysql> select * from student where name like '%李%';
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 2 | 李進 | 67 | 98 | 56 |
| 4 | 李一 | 88 | 98 | 90 |
| 5 | 李來財 | 82| 84 | 67 |
| 8 | 張一李 | 75 | 65 | 30 |
| 9 | 何李 | 75 | 65 | 30 |
| 11 | 李 | 75 | 65 | NULL |
+------+-----------+---------+---------+------+
6 rows in set(0.00 sec)
--查詢數學成績爲空的記錄
mysql> select * from student where math is null;
+------+------+---------+---------+------+
| id | name | chinese | english | math |
+------+------+---------+---------+------+
| 11 | 李 | 75 | 65 | NULL |
+------+------+---------+---------+------+
1 row in set(0.00 sec)
--查詢數學成績不爲空的記錄
mysql> select * from student where math is not null;
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
| 2 | 李進 | 67 | 98 | 56 |
| 3 | 王五 | 87 | 78 | 77 |
| 4 | 李一 | 88 | 98 | 90 |
| 5 | 李來財 | 82 | 84 | 67 |
| 6 | 張進寶 | 55 | 85 | 45 |
| 7 | 黃蓉 | 75 | 65 | 30 |
| 8 | 張一李 | 75 | 65 | 30 |
| 9 | 何李 | 75 | 65 | 30 |
| 10 | 單 | 75 | 65 | 30 |
| 12 | jack | 75 | 65 | 40 |
| 13 | marry | 75 | 65 | 60 |
+------+-----------+---------+---------+------+
12 rows in set(0.00 sec)
--查詢數學和語法大於80的記錄
mysql> select * from student where math>80 &&chinese>80;
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
| 4 | 李一 | 88 | 98 | 90 |
+------+-----------+---------+---------+------+
2 rows in set(0.00 sec)
mysql> select * from student where math>80 and chinese>80;
+------+-----------+---------+---------+------+
| id | name | chinese | english | math |
+------+-----------+---------+---------+------+
| 1 | 張小明 | 89 | 78 | 90 |
| 4 | 李一 | 88 | 98 | 90 |
+------+-----------+---------+---------+------+
2 rows in set(0.00 sec)
l 使用order by 子句排序查詢結果。
語法:
SELECT column1,column2. column3..
FROM table;
order by columnasc|desc
l Order by指定排序的列,排序的列便可是表中的列名,也能夠是select語句後指定的列名。
l Asc升序、Desc 降序
l ORDER BY子句應位於SELECT語句的結尾。
--按數學成績排序輸出desc降序
asc(默認升序)
mysql> select name,math from student order by math desc;
+-----------+------+
| name | math |
+-----------+------+
| 張小明 | 90 |
| 李一 | 90 |
| 王五 | 77 |
| 李來財 | 67 |
| marry | 60 |
| 李進 | 56 |
| 張進寶 | 45 |
| jack | 40 |
| 張一李 | 30 |
| 何李 | 30 |
| 單 | 30 |
| 黃蓉 | 30 |
| 李 | NULL |
+-----------+------+
13 rows in set(0.00 sec)
mysql> select name,math from student order by math;
+-----------+------+
| name | math |
+-----------+------+
| 李 | NULL |
| 黃蓉 | 30 |
| 單 | 30 |
| 何李 | 30 |
| 張一李 | 30 |
| jack | 40 |
| 張進寶 | 45 |
| 李進 | 56 |
| marry | 60 |
| 李來財 | 67|
| 王五 | 77 |
| 李一 | 90 |
| 張小明 | 90 |
+-----------+------+
13 rows in set(0.00 sec)
mysql> select name,math from student order by math asc;
+-----------+------+
| name | math |
+-----------+------+
| 李 | NULL |
| 黃蓉 | 30 |
| 單 | 30 |
| 何李 | 30 |
| 張一李 | 30 |
| jack | 40 |
| 張進寶 | 45 |
| 李進 | 56 |
| marry | 60 |
| 李來財 | 67 |
| 王五 | 77 |
| 李一 | 90 |
| 張小明 | 90 |
+-----------+------+
13 rows in set(0.00 sec)
--對總分降序輸出
mysql> selectname,(chinese+math+english) as總分 from student orderby (chinese+math+english) desc;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 李一 | 276 |
| 張小明 | 257 |
| 王五 | 242 |
| 李來財 | 233 |
| 李進 | 221 |
| marry | 200 |
| 張進寶 | 185 |
| jack | 180 |
| 張一李 | 170 |
| 何李 | 170 |
| 單 | 170 |
| 黃蓉 | 170 |
| 李 | NULL |
+-----------+--------+
13 rows in set (0.00 sec)
--對姓名的學生按總分進行排序
mysql> select name,math+chinese+englishas總分
-> from student
-> where name LIKE '李%'
-> order bymath+chinese+english desc;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 李一 | 276 |
| 李來財 | 233 |
| 李進 | 221 |
| 李 | NULL |
+-----------+--------+
4 rows in set (0.00 sec)
===================函數==================
l Count(列名)返回某一列,行的總數,除null外
語法:
Select count(*)|count(列名) fromtablename[WHERE where_definition]
--統計學生人數
mysql>select count(*) as學生人數 from student;
+--------------+
| 學生人數 |
+--------------+
| 13 |
+--------------+
1 row in set (0.00 sec)
--統計數學成績大於80的人數
mysql>select count(*) as數學大於80的人數from student where math>80
+-------------------------+
| 數學大於80的人數 |
+-------------------------+
| 2 |
+-------------------------+
1 row in set (0.00 sec)
--統計數學成績的人數,爲12,由於有一個null,count不進行統計
mysql> selectcount(math) from student;
+-------------+
| count(math) |
+-------------+
| 12 |
+-------------+
1 row in set (0.00 sec)
--統計學生數學的總分
l Sum函數返回知足where條件的行的和
Select sum(列名){,sum(列名)…} fromtablename [WHERE where_definition]
mysql> selectsum(math) as '數學總分' from student;
+--------------+
| 數學總分 |
+--------------+
| 645 |
+--------------+
1 row in set (0.00 sec)
mysql> selectsum(math)+ sum(chinese)+ sum(english)as '語數外總分' from student;
+-----------------+
| 語數外總分 |
+-----------------+
| 2614 |
+-----------------+
1 row in set (0.00 sec)
AVG函數返回知足where條件的一列的平均值
Select sum(列名){,sum(列名)…} from tablename
[WHERE where_definition]
--統計語文平均分
mysql> selectsum(chinese)/count(*) as '語文平均分'from student;
+-------------------+
| 語文平均分 |
+-------------------+
| 76.38461538461539 |
+-------------------+
1 row in set (0.00 sec)
mysql> selectavg(chinese) as '語文平均分'from student;
+-------------------+
| 語文平均分 |
+-------------------+
| 76.38461538461539 |
+-------------------+
1 row in set (0.00 sec)
--求班級總分平均數
mysql> select (sum(math)+sum(english)+sum(chinese))/count(*)as '總分平均數' from student;
+--------------------+
| 總分平均數 |
+--------------------+
| 201.07692307692307 |
+--------------------+
1 row in set (0.00 sec)
l Max/min函數返回知足where條件的一列的最大/最小值
語法:
Select max(列名)from tablename [WHERE where_definition]
Max/min函數返回知足where條件的一列的最大/最小值求班級最高分和最低分。(數值範圍在統計中特別有用)
max(),min(),當max()和min()函數位於日期類型時,分別取得最近日期和最先日期,用於varchar是按碼錶的順序進行查詢
mysql> select min(chinese) as語文最低分,max(chinese) as 語文最高分 from student;
+-----------------+-----------------+
| 語文最低分 | 語文最高分 |
+-----------------+-----------------+
| 55 | 89 |
+-----------------+-----------------+
1 row in set (0.00sec)
mysql> droptable if exists teacher;
Query OK, 0 rowsaffected, 1 warning (0.00 sec)
mysql> createtable teacher(
-> id int,
-> name varchar(20),
-> birthday date
-> );
Query OK, 0 rowsaffected (0.61 sec)
mysql> insertinto teacher(id,name,birthday) values(1,'jack','2011-1-1');
Query OK, 1 rowaffected (0.14 sec)
mysql> insertinto teacher(id,name,birthday) values(2,'marry','2011-2-2');
Query OK, 1 rowaffected (0.08 sec)
mysql> insertinto teacher(id,name,birthday) values(3,'sisi','2011-3-3');
Query OK, 1 rowaffected (0.53 sec)
mysql>
mysql> selectmax(birthday),min(birthday)
-> from teacher;
+---------------+---------------+
| max(birthday) |min(birthday) |
+---------------+---------------+
| 2011-03-03 | 2011-01-01 |
+---------------+---------------+
1 row in set (0.00sec)
mysql>
--order表
mysql> createtable orders(
-> id int,
-> product varchar(20),
-> price float
-> );
Query OK, 0 rowsaffected (0.90 sec)
mysql>
mysql> insertinto orders(id,product,price) values(1,'電視',900);
Query OK, 1 rowaffected (0.05 sec)
mysql> insert intoorders(id,product,price) values(2,'洗衣機',100);
Query OK, 1 rowaffected (0.06 sec)
mysql> insertinto orders(id,product,price) values(3,'洗衣粉',90);
Query OK, 1 rowaffected (0.44 sec)
mysql> insertinto orders(id,product,price) values(4,'桔子',10);
Query OK, 1 rowaffected (0.07 sec)
mysql> insertinto orders(id,product,price) values(5,'洗衣粉',80);
Query OK, 1 rowaffected (0.06 sec)
mysql>
mysql> select *from orders;
+------+-----------+-------+
| id | product | price |
+------+-----------+-------+
| 1 | 電視 | 900 |
| 2 | 洗衣機 | 100 |
| 3 | 洗衣粉 | 90 |
| 4 | 桔子 | 10 |
| 5 | 洗衣粉 | 80 |
+------+-----------+-------+
5 rows in set(0.00 sec)
l 使用group by 子句對列進行分組
語法:
SELECT column1,column2. column3.. FROM table;
group bycolumn
--對訂單表中商品歸類後,顯示每一類商品的總價
l 使用group by 子句對列進行分組
mysql> select product as類別名,sum(price) as商品類別總價
-> from orders
-> group by product;
+-----------+--------------------+
| 類別名 | 商品類別總價 |
+-----------+--------------------+
| 桔子 | 10 |
| 洗衣機 | 100 |
| 洗衣粉 | 170 |
| 電視 | 900 |
+-----------+--------------------+
4 rows in set(0.00 sec)
--查詢購買了幾類商品,而且每類總價大於100的商品
l 使用having 子句組過濾
mysql> select product as類別名,sum(price) as商品類別總價
-> from orders
-> group by product
-> having sum(price) > 100;
+-----------+--------------------+
| 類別名 | 商品類別總價 |
+-----------+--------------------+
| 洗衣粉 | 170 |
| 電視 | 900 |
+-----------+--------------------+
2 rows inset (0.00 sec)
l Having和where都可實現過濾,但在having可使用合計函數,having一般跟在group by後,它做用於組。
where主要用於行過濾器
having主要用於類別過濾器,一般有having就必定出現group by,但有group by的地方,不必定出現having。