SQL语句分类:
- DDL:
CREATE
,DROP
,ALTER
- DML:
INSERT
,DELETE
,UPDATE
- DCL:
GRANT
,REVOKE
,COMMIT
,ROLLBACK
- DQL:
SELECT
DQL语句
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
[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]]
- 字段显示可以使用别名:
#col1 AS alias1, col2 AS alias2, ...
MariaDB [hellodb]> select name as NNN,age as GGG from students;
+---------------+-----+
| NNN | GGG |
+---------------+-----+
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xie Yanke | 53 |
| Ding Dian | 32 |
+---------------+-----+
限制子句
- WHERE子句:
指明过滤条件以实现“选择”的功能:- 算术操作符:
+
,-
,*
,/
,%
- 比较操作符:
=
,<=>
(相等或都为空),<>
, !=(非标准SQL),>
,>=
,<
,<=
- BETWEEN min_num AND max_num #在min和max之间
- IN (element1, element2, ...)
- IS NULL
- IS NOT NULL
- 算术操作符:
#比较操作符
MariaDB [hellodb]> select * from students where stuid <> 1;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 2 | Shi Potian | 22 | M | 1 | 7 |
+-------+---------------+-----+--------+---------+-----------+
#IS NULL 和 IS NOT NULL
MariaDB [hellodb]> select * from students where classid is null;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+-------------+-----+--------+---------+-----------+
- ==DISTINCT==
去除重复列
SELECT DISTINCT gender FROM students;
- ==LIKE==:(模糊匹配)
%
任意长度的任意字符_
任意单个字符RLIKE
:正则表达式,索引失效,不建议使用REGEXP
:匹配字符串可用正则表达式书写模式,同上- 逻辑操作符:
NOT
、AND
、OR
、XOR
#and
MariaDB [hellodb]> select * from students where age > 10 and gender='f';
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
+-------+---------------+-----+--------+---------+-----------+
#like
MariaDB [hellodb]> select * from students where name like 's%';
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+-------------+-----+--------+---------+-----------+
其它条件
- ==GROUP==:
根据指定的条件把查询结果进行“分组”以用于做“聚合”运算avg()
,max()
,min()
,count()
,sum()
#统计非空的行数
MariaDB [hellodb]> select count(stuid) as NUM from students;
+-----+
| NUM |
+-----+
| 25 |
+-----+
- ==GROUP BY==
分组统计:一旦引用了group by以后,在select后跟的内容:分组的字段名+聚合函数
MariaDB [hellodb]> select gender,avg(age) as NUM from students group by gender;
+--------+---------+
| gender | NUM |
+--------+---------+
| F | 19.0000 |
| M | 33.0000 |
+--------+---------+
MariaDB [hellodb]> select classid,avg(age) from students where classid > 3 group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+----------+
- ==HAVING==:
对分组聚合运算后的结果指定过滤条件
对比where:where是分组前过滤,having是分组后过滤。
MariaDB [hellodb]> select classid,avg(age) from students group by classid having classid > 3;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+----------+
#SELECT * FROM * WHERE * GROUP BY * HAVING * ;
MariaDB [hellodb]> select classid,avg(age) as age from students where classid > 3 group by classid having age > 30;
+---------+---------+
| classid | age |
+---------+---------+
| 5 | 46.0000 |
+---------+---------+
- ==ORDER BY==
根据指定的字段对查询结果进行排序
升序:ASC (默认)
降序:DESC (也可以在字段前-
,如-name
)
MariaDB [hellodb]> select classid,sum(age) from students group by classid order by classid;
+---------+----------+
| classid | sum(age) |
+---------+----------+
| NULL | 127 |
| 1 | 82 |
| 2 | 108 |
| 3 | 81 |
| 4 | 99 |
| 5 | 46 |
| 6 | 83 |
| 7 | 59 |
+---------+----------+
- LIMIT [[offset,]row_count]
对查询的结果进行输出行数数量限制
#跳过前2个,查询3个
select classid,sum(age) from students group by classid order by classid limit 2,3;
- 对查询结果中的数据请求施加“锁”
FOR UPDATE: 写锁,独占或排它锁,只有一个读和写
LOCK IN SHARE MODE: 读锁,共享锁,同时多个读
总结:
SELECT * FROM * WHERE * GROUP BY * HAVING * ORDER BY * LIMIT * ;
SELECT示例
#查询所有字段
DESC students;
#往students表插入值
INSERT INTO students VALUES(1,'tom','m'),(2,'alice','f');
INSERT INTO students(id,name) VALUES(3,'jack'),(4,'allen');
#
SELECT id stuid,name as stuname FROM students
#where
SELECT * FROM students WHERE id < 3;
SELECT * FROM students WHERE gender='m';
SELECT * FROM students WHERE gender IS NULL;
SELECT * FROM students WHERE gender IS NOT NULL;
SELECT * FROM students WHERE id >=2 and id <=4
SELECT * FROM students WHERE BETWEEN 2 AND 4
SELECT * FROM students WHERE name LIKE ‘t%’
SELECT * FROM students WHERE name RLIKE '.*[lo].*';
#order by
SELECT * FROM students ORDER BY name DESC LIMIT 2;
SELECT * FROM students ORDER BY name DESC LIMIT 1,2;
案例:黑客攻击:SQL注入
- 假如我们有一个表,如下:
MariaDB [hellodb]> select * from user;
+------+----------+----------+
| id | username | password |
+------+----------+----------+
| 1 | admin | centos |
| 2 | wang | centos |
| 3 | chen | centos |
| 4 | li | centos |
+------+----------+----------+
4 rows in set (0.01 sec)
- 正常情况下,输入正确的帐号密码可以查询到数据,而错误的则没有。这样来到达用户登录的目的
MariaDB [hellodb]> select * from user where username="admin" and password="ma";
Empty set (0.00 sec)
MariaDB [hellodb]> select * from user where username="admin" and password="centos";
+------+----------+----------+
| id | username | password |
+------+----------+----------+
| 1 | admin | centos |
+------+----------+----------+
1 row in set (0.00 sec)
- 黑客攻击
构建特殊的帐号密码:admin
+'or'1'='1
或admin'--
+'
,也能查询到,从而绕过了安全查检。
MariaDB [hellodb]> select * from user where username="admin" and password=''or'1'='1';
+------+----------+----------+
| id | username | password |
+------+----------+----------+
| 1 | admin | centos |
| 2 | wang | centos |
| 3 | chen | centos |
| 4 | li | centos |
+------+----------+----------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> select * from user where username='admin'--' and password=''';
+------+----------+----------+
| id | username | password |
+------+----------+----------+
| 1 | admin | centos |
| 2 | wang | centos |
| 3 | chen | centos |
| 4 | li | centos |
+------+----------+----------+
4 rows in set, 6 warnings (0.00 sec)
UNION
联合查询,纵向合并
SELECT Name,Age FROM students UNION SELECT Name,Age FROM teachers;
如果字段不一样,则无法合并
#union默认去重,union all不去重
select name,age,gender,stuid from students union select * from students;