CREATE
,DROP
,ALTER
INSERT
,DELETE
,UPDATE
GRANT
,REVOKE
,COMMIT
,ROLLBACK
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 |
+---------------+-----+
+
, -
, *
, /
, %
=
,<=>
(相等或都为空),<>
, !=(非标准SQL), >
, >=
, <
, <=
#比较操作符
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 |
+-------+-------------+-----+--------+---------+-----------+
SELECT DISTINCT gender FROM students;
%
任意长度的任意字符_
任意单个字符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 |
+-------+-------------+-----+--------+---------+-----------+
avg()
, max()
, min()
, count()
, sum()
#统计非空的行数
MariaDB [hellodb]> select count(stuid) as NUM from students;
+-----+
| NUM |
+-----+
| 25 |
+-----+
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 |
+---------+----------+
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 |
+---------+---------+
-
,如-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 |
+---------+----------+
#跳过前2个,查询3个
select classid,sum(age) from students group by classid order by classid limit 2,3;
总结:
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;
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)
联合查询,纵向合并
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数据库(十三)_事务与锁