0%

mysql1

###前言
SQL语句主要分为三大类

DDL语句
DDL是数据定义语句,是对数据库内部的对象进行创建、删除、修改等操作的语句.create、drop、alter等(DBA常用)

DML语句
DML是数据操作语句,指对数据库表记录的基本操作,insert、update、delete、select等(开发常用)

DCL语句
DCL是数据控制语句,用于控制不同数据段直接的许可和访问级别的语句.定义了数据库、表、字段、用户的访问权限和安全级别.主要是grant、revoke等(DBA常用)

DML语句

插入记录

建好表之后,往表里边插入数据
语法如下:

1
INSERT INTO tablename (field1,field2,...,fieldn) VALUES(value1,value2,...,valuen);

一次插入多天数据
语法如下:

1
INSERT INTO tablename (field1,field2,...,fieldn) values(record1_value1,record1_value2,...,record1_valuen),(record2_value1,record2_value2,...,record2_valuen),(record3_value1,record3_value2,...,record3_valuen);

更新记录

表里添加的数据通过update进行更新,
语法如下:

1
UPDATE tablename SET field1=value1,field2=value2,...,fieldn=valuen [WHERE CONDITION];

update命令可以同时更新多个表中的数据
语法如下:

1
UPDATE t1,t2,...,tn SET t1.field1=expr1,tn.fieldn=exprn [WHERE CONDITION];

删除记录

查询记录

查询不重复记录

distinct

条件查询

where = < >= <= !=

排序和限制

order by 、 desc 、 asc、 limit

聚合

sum() 、 count() 、 max() 、 min() 、 group by、 with rollup、
语法如下:

1

表连接

从大类上分为内连接和外连接
外连接又分为左连接和右连接

  • 内外连接的区别:内连接仅选出两张表中相匹配的记录,而外连接会选出其他不匹配的记录.
  • 左连接:包含所有的左边的表记录甚至是右边表中没有和它匹配的记录.
  • 右连接:包含所有的右边表的记录甚至是左边表中没有和它匹配的记录.
    left jion on、
    right jion on
    语法如下:
    1
    2
    3
    4
    5
    6
    外连接:
    select * from t1,t2 where t1.filed1=t2.filed2;
    左连接:
    select * from t1 left join t2 on t1.filed1=t2.filed2;
    右连接:
    select * from t1 right join t2 on t1.filed1=t2.filed2;

子查询

in 、 not in、 =、 != 、 exists、 not exists
语法如下:

1
2
select * from t1 where filed1 in (select filed2 from t2);
如果子查询记录数唯一,可以用=代替in.
  • 子查询可以转换为表连接

记录联合

union 、union all
语法如下:

1
2
3
4
5
6
select * from t1 
union/union all
select * from t2
...
union/union all
select * from tm;
  • union和union all的主要区别是union all是把结果集直接合并在一起,而union 是将union all后的结果进行一次distinct,去除重复记录的结果.

练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
MariaDB [xe]> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | tech |
| 2 | sale |
| 5 | fin |
+--------+----------+
3 rows in set (0.00 sec)

MariaDB [xe]> select * from xe.emp;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx1 | 2000-01-01 | 100.00 | 1 |
| lisa | 2003-02-01 | 200.00 | 2 |
| bjguan | 2004-04-02 | 100.00 | 1 |
| dony | 2005-02-01 | 2000.00 | 4 |
+--------+------------+---------+--------+
4 rows in set (0.00 sec)

MariaDB [xe]> update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 5 Changed: 3 Warnings: 0

MariaDB [xe]> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | tech |
| 2 | sale |
| 5 | fin |
+--------+----------+
3 rows in set (0.00 sec)

MariaDB [xe]> select * from xe.emp;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx1 | 2000-01-01 | 100.00 | 1 |
| lisa | 2003-02-01 | 200.00 | 2 |
| bjguan | 2004-04-02 | 100.00 | 1 |
| dony | 2005-02-01 | 2000.00 | 4 |
+--------+------------+---------+--------+
4 rows in set (0.00 sec)

MariaDB [xe]> update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 5 Changed: 3 Warnings: 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
mysql> select * from emp;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx1 | 2000-01-01 | 100.00 | 1 |
| lisa | 2003-02-01 | 400.00 | 2 |
| bjguan | 2004-04-02 | 100.00 | 1 |
| dony | 2005-02-01 | 2000.00 | 4 |
+--------+------------+---------+--------+
4 rows in set (0.00 sec)

mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | zzx1 |
| 2 | lisa |
| 5 | fin |
+--------+----------+
3 rows in set (0.00 sec)
---------------------------------------------

mysql> select e.ename 名字,d.deptno 编号,e.sal 薪资 from xe.emp e LEFT JOIN xe.dept d on e.ename=d.deptname;
+--------+--------+---------+
| 名字 | 编号 | 薪资 |
+--------+--------+---------+
| zzx1 | 1 | 100.00 |
| lisa | 2 | 400.00 |
| bjguan | NULL | 100.00 |
| dony | NULL | 2000.00 |
+--------+--------+---------+
4 rows in set (0.00 sec)

mysql> select e.ename 名字,d.deptno 编号,e.sal 薪资 from xe.emp e LEFT JOIN xe.dept d on e.ename=d.deptname where e.sal > '300';
+--------+--------+---------+
| 名字 | 编号 | 薪资 |
+--------+--------+---------+
| lisa | 2 | 400.00 |
| dony | NULL | 2000.00 |
+--------+--------+---------+
2 rows in set (0.00 sec)
---------------------------------------

mysql> select e.ename '名字',d.deptno '编号',e.sal '薪资' from xe.emp e LEFT JOIN xe.dept d on e.ename=d.deptname where d.depptno != 'NULL';
+--------+--------+--------+
| 名字 | 编号 | 薪资 |
+--------+--------+--------+
| zzx1 | 1 | 100.00 |
| lisa | 2 | 400.00 |
+--------+--------+--------+
2 rows in set, 1 warning (0.00 sec)

mysql> select e.ename '名字',d.deptno '编号',e.sal '薪资' from xe.emp e LEFT JOIN xe.dept d on e.ename=d.deptname where d.depptno != 'NULL';
+--------+--------+--------+
| 名字 | 编号 | 薪资 |
+--------+--------+--------+
| zzx1 | 1 | 100.00 |
| lisa | 2 | 400.00 |
+--------+--------+--------+
2 rows in set, 1 warning (0.00 sec)