MySQL数据库(五)_SQL语句之DQL

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

SQL语句分类:

  1. DDLCREATEDROPALTER
  2. DMLINSERTDELETEUPDATE
  3. DCLGRANTREVOKECOMMITROLLBACK
  4. DQLSELECT

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]]
  1. 字段显示可以使用别名:
#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 |
+---------------+-----+

限制子句

  1. 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 |
+-------+-------------+-----+--------+---------+-----------+
  1. DISTINCT
    去除重复列
SELECT DISTINCT gender FROM students;
  1. LIKE:(模糊匹配)
    • % 任意长度的任意字符
    • _ 任意单个字符
    • RLIKE:正则表达式,索引失效,不建议使用
    • REGEXP:匹配字符串可用正则表达式书写模式,同上
    • 逻辑操作符:NOTANDORXOR
#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 |
+-------+-------------+-----+--------+---------+-----------+

其它条件

  1. GROUP
    根据指定的条件把查询结果进行“分组”以用于做“聚合”运算
    avg(), max(), min(), count(), sum()
#统计非空的行数
MariaDB [hellodb]> select count(stuid) as NUM from students;
+-----+
| NUM |
+-----+
|  25 |
+-----+
  1. 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 |
+---------+----------+
  1. 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 |
+---------+---------+
  1. 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 |
+---------+----------+
  1. LIMIT [[offset,]row_count]
    对查询的结果进行输出行数数量限制
#跳过前2个,查询3个
select classid,sum(age) from students group by classid order by classid limit 2,3;
  1. 对查询结果中的数据请求施加“锁”
    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注入

  1. 假如我们有一个表,如下:
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)
  1. 正常情况下,输入正确的帐号密码可以查询到数据,而错误的则没有。这样来到达用户登录的目的
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)
  1. 黑客攻击
    构建特殊的帐号密码:admin+'or'1'='1admin'--+',也能查询到,从而绕过了安全查检。
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;

其它章节
MySQL数据库(一)_基础概念
MySQL数据库(二)_SQL基础与数据类型
MySQL数据库(三)_SQL语句之DDL
MySQL数据库(四)_SQL语句之DML
MySQL数据库(五)_SQL语句之DQL
MySQL数据库(六)_SQL语句之JOIN
MySQL数据库(七)_视图、函数、储存过程及触发器
MySQL数据库(八)_用户管理
MySQL数据库(九)_存储引擎
MySQL数据库(十)_服务器配置
MySQL数据库(十一)_查询缓存
MySQL数据库(十二)_索引
MySQL数据库(十三)_事务与锁

猜你喜欢

转载自blog.csdn.net/weixin_42758707/article/details/95064697