您好,登錄后才能下訂單哦!
名稱 | 作用 | 語法關鍵字 |
---|---|---|
DDL | 數據定義語言 | CREATE,DROP,ALTER |
DML | 數據操縱語言 | INSERT,DELETE,UPDATE |
DQL | 數據查詢語言 | SELECT |
DCL | 數據控制語言 | GRANT,REVOKE,COMMIT,ROLLBACK |
類型 | 儲存空間(字節) | 范圍 |
---|---|---|
tinyint(m) | 1 | -128~127 |
smallint(m) | 2 | -32768~32767 |
mediumint(m) | 3 | -8388608~8388607 |
int(m) | 4 | -2147483648~2147483647 |
bigint(m) | 8 | -2^63~2^63-1 |
上述數據類型,如果加修飾符unsigned后,則最大值翻倍,如:tinyint unsigned的取值范圍為(0~255)
int(m)里的m是表示SELECT查詢結果集中的顯示寬度,并不影響實際的取值范圍,規定了MySQL的一些交互工具(例如MySQL命令行客戶端)用來顯示字符的個數。對于存儲和計算來說,Int(1)和Int(20)是相同的
BOOL,BOOLEAN:布爾型,是TINYINT(1)的同義詞。zero值被視為假,非zero值視為真
float(m,d)單精度浮點型8位精度(4字節)m總個數,d小數位
double(m,d)雙精度浮點型16位精度(8字節)m總個數,d小數位
假設一個字段定義為float(6,3),如果插入一個數123.45678,實際數據庫里存的是123.457,但總個數還以實際為準,即6位
在數據庫中存放的是精確值,存為十進制
decimal(m,d)參數m<65 是總個數,d<30且 d<m 是小數位
MySQL5.0和更高版本將數字打包保存到一個二進制字符串中(每4個字節存9個數字)。
例如:
decimal(18,9)小數點兩邊將各存儲9個數字,一共使用9個字節:其中,小數點前的9個數字用4個字節,小數點后的9個數字用4個字節,小數點本身占1個字節
浮點類型在存儲同樣范圍的值時,通常比decimal使用更少的空間。float使用4個字節存儲。double占用8個字節
因為需要額外的空間和計算開銷,所以應該盡量只在對小數進行精確計算時才使用decimal,例如存儲財務數據。但在數據量比較大的時候,可以考慮使用bigint代替decimal
數據類型 | 值 |
---|---|
DATE | '0000-00-00' |
TIME | '00:00:00' |
DATETIME | '0000-00-00 00:00:00' |
TIMESTAMP | '0000-00-00 00:00:00' |
YEAR(2),YEAR(4) | 00,0000 |
數據類型 | 值 |
---|---|
char(n) 固定長度 | 最多255個字符 |
varchar(n) 可變長度 | 最多65535個字符 |
tinytext 可變長度 | 最多255個字符 |
text 可變長度 | 最多65535個字符 |
mediumtext 可變長度 | 最多2的24次方-1個字符 |
longtext 可變長度 | 最多2的32次方-1個字符 |
BINARY(M) 固定長度 | 可存二進制或字符,長度為0-M字節 |
VARBINARY(M) 可變長度 | 可存二進制或字符,允許長度為0-M字節 |
內建類型:ENUM枚舉,SET集合
ENUM是一個字符串對象,其值是從允許值的列表中選擇的,這些值在表創建時在列規范中明確枚舉
SET是可以具有零個或多個值的字符串對象,每個值都必須從創建表時指定的允許值列表中選擇。 SET由多個set成員組成的列值用用逗號(,)分隔的成員指定。這樣的結果是 SET成員值本身不應包含逗號。
1.char(n) 若存入字符數小于n,則以空格補于其后,查詢之時再將空格去掉,所以char類型存儲的字符串末尾不能有空格,varchar不限于此
2.char(n) 固定長度,char(4)不管是存入幾個字符,都將占用4個字節,varchar是存入的實際字符數+1個字節(n< n>255),所以varchar(4),存入3個字符將占用4個字節
3.char類型的字符串檢索速度要比varchar類型的快
1.varchar可指定n,text不能指定,內部存儲varchar是存入的實際字符數+1個字節(n< n>255),text是實際字符數+2個字節。
2.text類型不能有默認值
3.varchar可直接創建索引,text創建索引要指定前多少個字符。varchar查詢速度快于text
數據類型
1.BLOB和test存儲方式不同,text以文本方式存儲,英文存儲區分大小寫,而blob以二進制方式存儲,不區分大小寫
2.BLOB存儲的數據只能整體讀出
3.text可以指定字符集,blob不用指定字符集
名稱 | 作用 |
---|---|
NULL | 數據列可包含NULL值 |
NOT NULL | 數據列不允許包含NULL值 |
DEFAULT | 默認值 |
PRIMARY KEY | 主鍵 |
UNIQUE KEY | 唯一鍵 |
CHARACTER SET name | 指定一個字符集 |
名稱 | 作用 |
---|---|
AUTO_INCREMENT | 自動遞增,適用于整數類型,不支持列的負值。 |
UNSIGNED | 無符號,可用于僅允許一列中使用非負數,或者在您需要該列的較大的較高數字范圍時使用 |
HELP CREATE TALBE;
CREATE TABLE [IF NOT EXISTS] ‘tbl_name’ (col1 type1 修飾符, col2 type2 修飾符,
...)
#字段信息
col type1
PRIMARY KEY(col1,...)
INDEX(col1, ...)
UNIQUE KEY(col1, ...)
#表選項:
ENGINE [=] engine_name
ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
注意:
范例:
mysql> CREATE TABLE student (id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(20) NOT NULL,age TINYINT UNSIGNED);
Query OK, 0 rows affected (0.03 sec)
mysql> DESC student;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> CREATE TABLE employee (id int UNSIGNED NOT NULL,name VARCHAR(20) NOT NULL,age TINYINT UNSIGNED,PRIMARY KEY(id,name));
Query OK, 0 rows affected (0.02 sec)
語法:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options]
[partition_options] select_statement
【例】通過查詢現存表student的數據來創建新表new_student:
mysql> desc student;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoming | 18 |
| 2 | xiaohong | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> create table new_student select * from student;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from new_student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoming | 18 |
| 2 | xiaohong | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> desc new_student;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | 0 | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
語法:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name { LIKE old_tbl_name | (LIKE
old_tbl_name) }
【例】通過查詢現存表student的數據來創建新表new2_student:
mysql> desc student;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoming | 18 |
| 2 | xiaohong | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> create table new2_student like student;
Query OK, 0 rows affected (0.30 sec)
mysql> desc new2_student;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> select * from new2_student;
Empty set (0.00 sec)
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
SHOW TABLES [FROM db_name]
DESC [db_name.]tb_name
SHOW COLUMNS FROM [db_name.]tb_name
SHOW CREATE TABLE tbl_name;
SHOW TABLE STATUS LIKE 'tbl_name';
show table status from db_name;
DROP TABLE [IF EXISTS] 'tbl_name';
語法:
ALTER TABLE tbl_name
獲取幫助:
help alter table
# 在表中添加字段:add
語法:
ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
# 刪除表中字段:drop
語法:
DROP [COLUMN] col_name
# 修改表中字段:
change(字段名)
語法:
CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
modify(字段屬性)
MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
mysql> alter table student rename s1;
Query OK, 0 rows affected (0.01 sec)
mysql> desc s1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table s1 add phone varchar(11) after name;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc s1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| phone | varchar(11) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table s1 modify phone int;
Query OK, 2 rows affected (7.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc s1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| phone | int(11) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table s1 change column phone mobile char(11);
Query OK, 2 rows affected (0.32 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc s1;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| mobile | char(11) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table s1 drop column mobile;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc s1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table s1 character set utf8;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table s1 change name new_name varchar(20) character set utf8;
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
INSERT,DELETE,UPDATE
功能:一次插入一行或多行數據
獲取幫助:help insert
簡化語法:
INSERT tbl_name [(col1,...)] VALUES (val1,...), (val2,...)
獲取幫助:help update
語法:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET assignment_list
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
mysql -U | --safe-updates| --i-am-a-dummy
mysql> select * from s1;
+----+----------+------+-------+
| id | new_name | age | geder |
+----+----------+------+-------+
| 1 | xiaoming | 22 | NULL |
| 2 | xiaohong | 20 | NULL |
+----+----------+------+-------+
2 rows in set (0.00 sec)
mysql> update s1 set age=25 ;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from s1;
+----+----------+------+-------+
| id | new_name | age | geder |
+----+----------+------+-------+
| 1 | xiaoming | 25 | NULL |
| 2 | xiaohong | 25 | NULL |
+----+----------+------+-------+
2 rows in set (0.00 sec)
mysql> update s1 set age=18 where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from s1;
+----+----------+------+-------+
| id | new_name | age | geder |
+----+----------+------+-------+
| 1 | xiaoming | 18 | NULL |
| 2 | xiaohong | 25 | NULL |
+----+----------+------+-------+
2 rows in set (0.00 sec)
[root@CentOS7-01 ~]#mysql -U -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mytest
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from s1;
+----+----------+------+-------+
| id | new_name | age | geder |
+----+----------+------+-------+
| 1 | xiaoming | 18 | NULL |
| 2 | xiaohong | 25 | NULL |
+----+----------+------+-------+
2 rows in set (0.00 sec)
mysql> update s1 set age=30;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
mysql> update s1 set age=30 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
獲取幫助:help delete;
語法:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
truncate table tbl_name;
獲取幫助:help select
語法:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
1)字段顯示可以使用別名
字段1 AS 字段1別名,字段2 AS 字段2別名
2)WHERE字句:指明過濾條件以實現“選擇”的功能。
過濾條件:布爾型表達式
算術操作符:+,-,*,/,%
比較操作符:=,<=>(相等或都為空),<>,!=(非標準SQL),>,>=,<,<=
BETWEEN min_num AND max_num # 在兩個值之間
IN(element1,element2,...) # ()中的任意單個值,相當于列表
IS NULL #為空
IS NOT NULL #不為空
DISTINCT #去除重復列,范例:SELECT DISTINCT gender FROM students;
LIKE:
% 任意長度的任意字符
_ 任意單個字符
RLIKE:正則表達式,索引失效,不建議使用
REGEXP:匹配字符串可用正則表達式書寫模式,同上
邏輯操作符:NOT,AND,OR,XOR
3)GROUP:根據指定的條件把查詢結果進行“分組”以用于做“聚合”運算,先過濾再分組
常見聚合函數:avg(), max(), min(), count(), sum()
HAVING: 對分組聚合運算后的結果指定過濾條件,先分組再過濾
4)ORDER BY: 根據指定的字段對查詢結果進行排序
升序:ASC
降序:DESC
5)LIMIT [[offset,]row_count]:對查詢的結果進行輸出行數數量限制
6)對查詢結果中的數據請求施加“鎖”
FOR UPDATE: 寫鎖,獨占或排它鎖,只有一個讀和寫操作
LOCK IN SHARE MODE: 讀鎖,共享鎖,同時多個讀操作
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | NULL | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | NULL | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 45 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
+-------+---------------+-----+--------+---------+-----------+
mysql> select name,age,gender from students where age > 25 and gender='m';
+-----------+-----+--------+
| name | age | gender |
+-----------+-----+--------+
| Xie Yanke | 53 | M |
| Ding Dian | 32 | M |
| Yu Yutong | 26 | M |
| Shi Qing | 46 | M |
+-----------+-----+--------+
4 rows in set (0.00 sec)
mysql> select classid,avg(age) from students group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| NULL | 36.0000 |
| 1 | 22.0000 |
| 2 | 22.0000 |
| 3 | 35.5000 |
| 4 | 32.0000 |
| 5 | 46.0000 |
| 6 | 20.0000 |
| 7 | 17.0000 |
+---------+----------+
8 rows in set (0.00 sec)
mysql> select classid,avg(age) from students where age>30 group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| NULL | 53.0000 |
| 3 | 45.0000 |
| 4 | 32.0000 |
| 5 | 46.0000 |
+---------+----------+
4 rows in set (0.00 sec)
mysql> select * from students where name like 'l%';
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
+-------+-----------+-----+--------+---------+-----------+
1 row in set (0.00 sec)
mysql> select * from students where teacherid is not null;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | NULL | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
+-------+-------------+-----+--------+---------+-----------+
5 rows in set (0.00 sec)
mysql> select * from students order by age desc limit 3;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 3 | Xie Yanke | 53 | M | NULL | 16 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 10 | Yue Lingshan | 45 | F | 3 | NULL |
+-------+--------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
mysql> select * from students where age between 20 and 25;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
+-------+--------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
mysql> select classid as 班級id,count(stuid) as 班級人數 from students group by classid;
+----------+--------------+
| 班級id | 班級人數 |
+----------+--------------+
| NULL | 2 |
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
| 7 | 1 |
+----------+--------------+
8 rows in set (0.00 sec)
mysql> select gender 性別,sum(age) 年齡之和 from students group by gender;
+--------+--------------+
| 性別 | 年齡之和 |
+--------+--------------+
| F | 101 |
| M | 201 |
+--------+--------------+
2 rows in set (0.00 sec)
mysql> select classid,avg(age) 平均年齡 from students group by classid having 平均年齡 > 25;
+---------+--------------+
| classid | 平均年齡 |
+---------+--------------+
| NULL | 36.0000 |
| 3 | 35.5000 |
| 4 | 32.0000 |
| 5 | 46.0000 |
+---------+--------------+
4 rows in set (0.00 sec)
mysql> select gender,sum(age) from students where age > 25 group by gender;
+--------+----------+
| gender | sum(age) |
+--------+----------+
| F | 45 |
| M | 157 |
+--------+----------+
2 rows in set (0.00 sec
mysql> select name,age from students where age > (select avg(age) from students);
+--------------+-----+
| name | age |
+--------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Shi Qing | 46 |
| Yue Lingshan | 45 |
+--------------+-----+
4 rows in set (0.00 sec)
mysql> select * from students order by -classid desc;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 10 | Yue Lingshan | 45 | F | 3 | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 3 | Xie Yanke | 53 | M | NULL | 16 |
| 7 | Xi Ren | 19 | F | NULL | NULL |
+-------+--------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
+----+-------+--------+----------+
| id | name | gender | password |
+----+-------+--------+----------+
| 2 | root | M | 88888 |
| 3 | admin | M | 66666 |
+----+-------+--------+----------+
【例1】
mysql> select * from user where name='admin' and password='' or 1=1;
+----+-------+--------+----------+
| id | name | gender | password |
+----+-------+--------+----------+
| 2 | root | M | 88888 |
| 3 | admin | M | 66666 |
+----+-------+--------+----------+
2 rows in set (0.01 sec)
【例2】
mysql> select * from user where name='admin'; -- and password='abc123';
+----+-------+--------+----------+
| id | name | gender | password |
+----+-------+--------+----------+
| 3 | admin | M | 66666 |
+----+-------+--------+----------+
1 row in set (0.00 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。