SQL语言的分类
- DQL(Data. Query Language):数据查询语言
select - DML(Date Manipulate Language):数据操作语言
insert, update ,delete - DDL(Date Define Langage):数据定义语言
create,drop,alter - TCL(Transaction Control Language):事物控制语言
commit,rollback
DQL之简单查询
语法:
SELECT 要查询的东西 [from 表名];
- 注意:
- 1.通过select 查询的结果,是一个虚拟的表格,不是真实的存在。
mysql> use myEmployees;
mysql> show tables;
+-----------------------+
| Tables_in_myemployees |
+-----------------------+
| departments |
| employees |
| jobs |
| locations |
+-----------------------+
4 rows in set (0.00 sec)
mysql> select * from departments;
+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Adm | 200 | 1700 |
| 20 | Mar | 201 | 1800 |
| 30 | Pur | 114 | 1700 |
| 40 | Hum | 203 | 2400 |
| 50 | Shi | 121 | 1500 |
| 60 | IT | 103 | 1400 |
| 70 | Pub | 204 | 2700 |
| 80 | Sal | 145 | 2500 |
| 90 | Exe | 100 | 1700 |
| 100 | Fin | 108 | 1700 |
| 110 | Acc | 205 | 1700 |
| 120 | Tre | NULL | 1700 |
| 130 | Cor | NULL | 1700 |
| 140 | Con | NULL | 1700 |
| 150 | Sha | NULL | 1700 |
| 160 | Ben | NULL | 1700 |
| 170 | Man | NULL | 1700 |
| 180 | Con | NULL | 1700 |
| 190 | Con | NULL | 1700 |
| 200 | Ope | NULL | 1700 |
| 210 | IT | NULL | 1700 |
| 220 | NOC | NULL | 1700 |
| 230 | IT | NULL | 1700 |
| 240 | Gov | NULL | 1700 |
| 250 | Ret | NULL | 1700 |
| 260 | Rec | NULL | 1700 |
| 270 | Pay | NULL | 1700 |
+---------------+-----------------+------------+-------------+
27 rows in set (0.00 sec)
- 2.要查询的东西,可以是常量值,可以是表达式,可以是字段,可以是函数
mysql> #查询常量
mysql> select 100;
+-----+
| 100 |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
mysql> #查询表达式
mysql> select 100%49;
+--------+
| 100%49 |
+--------+
| 2 |
+--------+
1 row in set (0.00 sec)
mysql> #查询函数
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.11 |
+-----------+
1 row in set (0.00 sec)
mysql>
补充内容
-
起别名:1.便于查询。2,解决查询的字段有重名的情况,使用别名区别开来。
AS可省略
mysql> select 100%49 AS 结果;
+--------+
| 结果 |
+--------+
| 2 |
+--------+
1 row in set (0.00 sec)
mysql>#AS 可省略
mysql> select last_name AS 姓, first_name AS 名 from employees;
+-------------+-------------+
| 姓 | 名 |
+-------------+-------------+
| K_ing | Steven |
| Kochhar | Neena |
| De Haan | Lex |
| Hunold | Alexander |
| Ernst | Bruce |
| Austin | David |
| Pataballa | Valli |
| Lorentz | Diana |
| Greenberg | Nancy |
| Faviet | Daniel |
| Chen | John |
| Sciarra | Ismael |
...
...
注意:
当别名含有特殊字符时(比如含有#,空格等),需要用双引号扩起来。
mysql> select salary as"out put" from employees;
+----------+
| out put |
+----------+
| 24000.00 |
| 17000.00 |
| 17000.00 |
| 9000.00 |
| 6000.00 |
| 4800.00 |
| 4800.00 |
| 4200.00 |
| 12000.00 |
| 9000.00
...
...
-
去除查询的重复结果:
select distinct 字段 from 表名;
mysql> select distinct department_id from employees;
+---------------+
| department_id |
+---------------+
| NULL |
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
| 70 |
| 80 |
| 90 |
| 100 |
| 110 |
+---------------+
12 rows in set (0.08 sec)
-
➕号的作用:
-
mysql中的加号只有一个功能:运算符
eg: 1. select 100+90;结果为190。两个操作数都为数值型,则做加法运算。
2.select '123' +90;结果为:213。当其中一方为字符型时,试图将字符型转换成数值型,如果转换成功,则继续做加法运算。select 'mary'+90 ;结果为90。如果转换失败,则将字符型数值转换成0.
3.select null+90;只要一方为null,则结果肯定为null;
-
-
连接符
- 在mysql中,要把东西连接起来(字段等),有个专门的函数。CONCAT(字符1,字符2,字符3...);
- eg:select concat('a', 'b','c'); //结果为:abc;
- 在mysql中,要把东西连接起来(字段等),有个专门的函数。CONCAT(字符1,字符2,字符3...);
-
ifnull() :判断某字段是否为Null,如果为空则返回指定的值,否者返回原来的值。
- eg:select ifnull(commission_pct,0) from employees;
注:这是本人的学习笔记,如果有错误的地方望指出一起讨论,谢谢!