首页 > 技术文章 > mysql基础操作(二)

xiaopeng01 2019-06-19 18:30 原文

1.插入记录

  insert into tablename(field1,field2,....fieldn) values(value1,value2,......value);

  例: 

mysql> insert into emp(ename,hiredate,sal,deptno) values('zzx1','2000-01-01','2000',1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values('lisa','2003-02-01','3000',2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp(ename,sal) values('dony',1000);
Query OK, 1 row affected (0.01 sec)

  插入多条记录:

    insert into tablename (field1,field2,...,fieldn)

    values

    (record1_value1,record1_value2,...,record1_valuesn),

    (record2_value1,record2_value2,...record2_valuesn),

    ....

    (recordn_value1,recordn_value2,....recordn_valuesn);

    例:

mysql> insert into dept values(5,'dept5'),(6,'dept6');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

2.更新记录

  update tablename set field1=value1,field2=value2,....fieldn=valuen[where condition]

  例:

mysql> update emp set sal=4000 where ename='lisa';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

更新多个表中数据:

update t1,t2,...tn set t1.field1=expr1,tn.fieldn=expn [where condition]

例:

  

mysql> 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.00 sec)
Rows matched: 5 Changed: 3 Warnings: 0

注意:

  多表更新的语法更多地用在了根据一个表的字段来动态地更新另外一个表的字段。

3删除记录

  delete from tablename [where condition]

  例:

mysql> delete from emp where ename='yan';
Query OK, 1 row affected (0.00 sec)

删除多表记录:

  delete t1,t2,...tn from t1,t2,..,tn[where condition]

  例:

mysql> delete a,b from emp a,dept b where a.deptno=b.deptno and a.deptno=2;
Query OK, 2 rows affected (0.00 sec)

注意:

  不管是单表还是多表,不加where条件将会把表的所有记录删除,所以操作时一定要小心。

4.查询记录

  select * from tablename [ where condition]

  例:

mysql> select * from emp;

  1.查询不重复记录:

    可以用关键字:distinct

    例: 

mysql> select distinct deptno from emp;
+--------+
| deptno |
+--------+
| 1 |
| 4 |
+--------+
2 rows in set (0.00 sec)

  2.条件查询:

    where关键字

    例: 

mysql> select * from emp where deptno=1;
+--------+------------+--------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+--------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
| bjguan | 2004-04-02 | 100.00 | 1 |
+--------+------------+--------+--------+
2 rows in set (0.00 sec)

where后面的条件是一个字段的=比较。除了=外,还可以使用>、<、>=、<=、!=等比较运算符;多个条件之间可以使用or、and等。

例:  

mysql> select * from emp where deptno=1 and sal<200;
+-------+------------+--------+--------+
| ename | hiredate | sal | deptno |
+-------+------------+--------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
+-------+------------+--------+--------+
1 row in set (0.00 sec)

   3.排序和限制:

    关键字:order by

    select * from tablename [where condotion] [order by field1 [desc|asc],filed2[desc|asc],...,fieldn[desc|asc]]

    其中,desc和asc是排序顺序关键字,desc表示按照字段进行降序排列,asc则表示升序排列,如果不写此关键字默认是升序排列。

    例: 

mysql> select * from emp order by sal;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
| bjguan | 2004-04-02 | 700.00 | 1 |
| dony | 2005-02-05 | 2000.00 | 4 |
+--------+------------+---------+--------+
3 rows in set (0.00 sec)

 

mysql> select * from emp order by deptno ,sal desc;
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| bjguan | 2004-04-02 | 700.00 | 1 |
| zzx | 2000-01-01 | 100.00 | 1 |
| dony | 2005-02-05 | 2000.00 | 4 |
+--------+------------+---------+--------+
3 rows in set (0.00 sec)

    limit关键字:

      只显示一部分,而不是全部。

      例: 

mysql> select * from emp order by sal limit 2;
+--------+------------+--------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+--------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
| bjguan | 2004-04-02 | 700.00 | 1 |
+--------+------------+--------+--------+
2 rows in set (0.00 sec)

注意:

   limit经常和order by 一起配合使用来进行记录的分页显示。

  4.聚合

    语法:

      select [field1,field2,...fieldn]fun_name from tablename [where where_contition] [group by field1,field2,...fieldn [with rollup]] [having where_contition]

    参数说明:

      • fun_name:表示要做的聚合操作,也就是聚合函数,常用的有sum(求和)、count(*)(记录数)、max(最大值)、min(最小值)。
      • group by:关键字表示要进行分类聚合的字段。
      • with rollup:是可选语法,表明是否对分类聚合后的结果进行再汇总。
      • having:关键字表示对分类后的结果再进行条件的过滤。

       注意:

          having和where的区别在于,having是对聚合后的结果进行条件的过滤,而where是在聚合前就对记录进行过滤,如果逻辑允许,我们尽可能用where先过滤记录,这样因为结果集减小 ,将对聚合的效率大大提高,最后再根据逻辑看是否用having进行再过滤。

    例:  

mysql> select count(*) from emp;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)

 

mysql> select deptno,count(1) from emp group by deptno;
+--------+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 2 |
| 4 | 1 |
+--------+----------+
2 rows in set (0.00 sec)

mysql> select deptno,count(1) from emp group by deptno with rollup;
+--------+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 2 |
| 4 | 1 |
| NULL | 3 |
+--------+----------+
3 rows in set (0.00 sec)

mysql> select deptno,count(1) from emp group by deptno having count(1) > 1;
+--------+----------+
| deptno | count(1) |
+--------+----------+
| 1 | 2 |
+--------+----------+
1 row in set (0.00 sec)

5.表连接

6.子查询

  用于子查询的关键字注意包括in、not in、=、!=、exists、not exists等。

  例:   

mysql> select * from emp where deptno in (select deptno from dept);
+--------+------------+---------+--------+
| ename | hiredate | sal | deptno |
+--------+------------+---------+--------+
| zzx | 2000-01-01 | 100.00 | 1 |
| bjguan | 2004-04-02 | 700.00 | 1 |
| hao | 2006-06-06 | 1000.00 | 5 |
+--------+------------+---------+--------+
3 rows in set (0.00 sec)

7.记录联合

  将两个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示出来,需要关键字union和union all

  语法:

    select * from t1  

    union | union all

    ...

    select * from t2

    union | union all

    select * from tn;

  union和union all 主要区别是union all 是把结果集直接合并在一起,而union是将union all 后的结果进行一次distinct,去除重复记录后的结果。

  例:    

mysql> select deptno from emp
-> union all
-> select deptno from dept;
+--------+
| deptno |
+--------+
| 1 |
| 4 |
| 1 |
| 5 |
| 1 |
| 5 |
+--------+
6 rows in set (0.00 sec)

mysql> select deptno from emp
-> union
-> select deptno from dept;
+--------+
| deptno |
+--------+
| 1 |
| 4 |
| 5 |
+--------+
3 rows in set (0.00 sec)

 

    

     

 

推荐阅读