第三章 DQL 数据查询---10种查询方式

1.查询语言:(以当前表名为emp为例)

(1)简单查询

查询所有的列//create  *  from  表名;

mysql> select * from emp;
+----+----------+--------+------------+----------+------------+----------+
| id | name     | gender | birthday   | salary   | entry_date | resume   |
+----+----------+--------+------------+----------+------------+----------+
|  1 | zhangsan | female | 1990-05-10 | 10000.00 | 2015-05-05 | goodgirl |
|  2 | lisi     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy |
|  3 | 你好     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy |
+----+----------+--------+------------+----------+------------+----------+
3 rows in set (0.00 sec)

查询指定列//select  name,birthday from emp;

mysql> select name,birthday from emp;
+----------+------------+
| name     | birthday   |
+----------+------------+
| zhangsan | 1990-05-10 |
| lisi     | 1995-05-10 |
| 你好     | 1995-05-10 |
+----------+------------+
3 rows in set (0.00 sec)

(2)条件查询

条件查询就是在查询时给出where子句,在where子句中可以使用如下运算符和关键字:

1.运算符(=、!=、<、>、<=、>=、<>)

2.关键字

between ..and...;例查询1是否在0和3之间

mysql> select 1 between 0 and 3;
+-------------------+
| 1 between 0 and 3 |
+-------------------+
|                 1 |/*!1 显示为true*/
+-------------------+
1 row in set (0.00 sec)

in(set);//判断是否在这个集合中

mysql> select 2 in(0,2);//2为该集合中的元素
+-----------+
| 2 in(0,2) |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

is   null;//例查询emp中name为null的记录

mysql> select * from emp where name is null;//当前显示为name没有为null的
Empty set (0.00 sec)

and;&;|;!   的使用

mysql> select * from emp where salary>800 and salary<10000;
+----+-------+--------+------------+---------+------------+----------+
| id | name  | gender | birthday   | salary  | entry_date | resume   |
+----+-------+--------+------------+---------+------------+----------+
|  3 | lihua | male   | 1995-05-10 | 1800.00 | 2015-05-05 | good boy |
+----+-------+--------+------------+---------+------------+----------+
1 row in set (0.00 sec)
mysql> select * from emp where resume!='goodgirl';
+----+-------+--------+------------+----------+------------+----------+
| id | name  | gender | birthday   | salary   | entry_date | resume   |
+----+-------+--------+------------+----------+------------+----------+
|  2 | lisi  | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy |
|  3 | lihua | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy |
+----+-------+--------+------------+----------+------------+----------+
2 rows in set (0.00 sec)
mysql> select * from emp where resume<>'goodgirl';
+----+-------+--------+------------+----------+------------+----------+
| id | name  | gender | birthday   | salary   | entry_date | resume   |
+----+-------+--------+------------+----------+------------+----------+
|  2 | lisi  | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy |
|  3 | lihua | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy |
+----+-------+--------+------------+----------+------------+----------+
2 rows in set (0.00 sec)

(3)模糊查询

当想查询姓名中包含a字母的人时就需要使用模糊查询了。模糊查询需要使用关键字like。

通配符:_   :代表一位     %:代表多位

mysql> select * from emp where name like '_i%';
+----+-------+--------+------------+----------+------------+----------+
| id | name  | gender | birthday   | salary   | entry_date | resume   |
+----+-------+--------+------------+----------+------------+----------+
|  2 | lisi  | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy |
|  3 | lihua | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy |
+----+-------+--------+------------+----------+------------+----------+
2 rows in set (0.00 sec)
mysql> select * from emp where name like 'z%';
+----+----------+--------+------------+--------+------------+----------+
| id | name     | gender | birthday   | salary | entry_date | resume   |
+----+----------+--------+------------+--------+------------+----------+
|  1 | zhangsan | female | 1990-05-10 | 800.00 | 2015-05-05 | goodgirl |
+----+----------+--------+------------+--------+------------+----------+
1 row in set (0.00 sec)

(4)字段控制查询

1.去除重复记录(两行或两行以上记录系列上的数据都相同),例上表中的resume中的goodboy,当查询emp表的resume字段时,那么会重复记录,那么想去除重复记录,需要使用distinct:

mysql> select distinct resume from emp;
+----------+
| resume   |
+----------+
| goodgirl |
| good boy |
+----------+
2 rows in set (0.00 sec)

2.先将上表中添加一列comm默认值为1000;查看salary列和comm列之和。因为salary和comm列都为int型,所以可以做加法运算,如果其中一列不是int型,那么会出错。

mysql> select *,salary+comm from emp;
+----+----------+--------+------------+----------+------------+----------+------+-------------+
| id | name     | gender | birthday   | salary   | entry_date | resume   | comm | salary+comm |
+----+----------+--------+------------+----------+------------+----------+------+-------------+
|  1 | zhangsan | female | 1990-05-10 |   800.00 | 2015-05-05 | goodgirl | 1000 |     1800.00 |
|  2 | lisi     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 |    11000.00 |
|  3 | lihua    | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |     2800.00 |
+----+----------+--------+------------+----------+------------+----------+------+-------------+
3 rows in set (0.00 sec)

3.给列名添加别名(将上表中的salary+comm列更名为num)  其中as 可以省略

mysql> select *,salary+comm as num from emp;
+----+----------+--------+------------+----------+------------+----------+------+----------+
| id | name     | gender | birthday   | salary   | entry_date | resume   | comm | num      |
+----+----------+--------+------------+----------+------------+----------+------+----------+
|  1 | zhangsan | female | 1990-05-10 |   800.00 | 2015-05-05 | goodgirl | 1000 |  1800.00 |
|  2 | lisi     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 | 11000.00 |
|  3 | lihua    | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |  2800.00 |
+----+----------+--------+------------+----------+------------+----------+------+----------+
3 rows in set (0.00 sec)

(5)排序

1.查询salary列 ,按数值从小到大排列(select *  from emp order by  列名  asc;  )

mysql> select * from emp order by salary asc;
+----+----------+--------+------------+----------+------------+----------+------+
| id | name     | gender | birthday   | salary   | entry_date | resume   | comm |
+----+----------+--------+------------+----------+------------+----------+------+
|  1 | zhangsan | female | 1990-05-10 |   800.00 | 2015-05-05 | goodgirl | 1000 |
|  3 | lihua    | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |
|  2 | lisi     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 |
+----+----------+--------+------------+----------+------------+----------+------+
3 rows in set (0.00 sec)

或者 (select   *  from  emp  order by  salary;)

mysql> select * from emp order by salary;
+----+----------+--------+------------+----------+------------+----------+------+
| id | name     | gender | birthday   | salary   | entry_date | resume   | comm |
+----+----------+--------+------------+----------+------------+----------+------+
|  1 | zhangsan | female | 1990-05-10 |   800.00 | 2015-05-05 | goodgirl | 1000 |
|  3 | lihua    | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |
|  2 | lisi     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 |
+----+----------+--------+------------+----------+------------+----------+------+
3 rows in set (0.00 sec)

  2.按salary列的从大到小排列(select  *  from  emp  order  by  desc;)

mysql> select * from emp order by salary desc;
+----+----------+--------+------------+----------+------------+----------+------+
| id | name     | gender | birthday   | salary   | entry_date | resume   | comm |
+----+----------+--------+------------+----------+------------+----------+------+
|  2 | lisi     | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 |
|  3 | lihua    | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |
|  1 | zhangsan | female | 1990-05-10 |   800.00 | 2015-05-05 | goodgirl | 1000 |
+----+----------+--------+------------+----------+------------+----------+------+
3 rows in set (0.00 sec)

3.查看所有人,按照salary列从大到小排序,如果 resume列相同,则按name列排序(select  *  from  emp  where  resume=’goodboy‘   order  by  name desc;)

mysql> select * from emp where resume='good boy' order by name desc;//降序
+----+-------+--------+------------+----------+------------+----------+------+
| id | name  | gender | birthday   | salary   | entry_date | resume   | comm |
+----+-------+--------+------------+----------+------------+----------+------+
|  2 | lisi  | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 |
|  3 | lihua | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |
+----+-------+--------+------------+----------+------------+----------+------+
2 rows in set (0.00 sec)

mysql> select * from emp where resume='good boy' order by name ;//升序
+----+-------+--------+------------+----------+------------+----------+------+
| id | name  | gender | birthday   | salary   | entry_date | resume   | comm |
+----+-------+--------+------------+----------+------------+----------+------+
|  3 | lihua | male   | 1995-05-10 |  1800.00 | 2015-05-05 | good boy | 1000 |
|  2 | lisi  | male   | 1995-05-10 | 10000.00 | 2015-05-05 | good boy | 1000 |
+----+-------+--------+------------+----------+------------+----------+------+
2 rows in set (0.00 sec)

(6)聚合函数:聚合函数都只有一个结果

聚合函数是用来做做纵向运算的函数:

1.count():统计指定列不为null的记录行数;

2.max():计算指定列的最大值,如果指定列为字符串类型,那么使用字符串排序运算;

3.min():计算指定列的最小值,如果指定列为字符串类型,那么使用字符串排序运算;

4.sum():计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0;

5.avg():计算指定列的平均值,如果指定列的类型不是数值类型,那么计算结果为0;

代码如下:

mysql> select count(*) as name from emp;//count()统计指定列不为null的个数
+------+
| name |
+------+
|    3 |
+------+
1 row in set (0.00 sec)
mysql> select max(salary) from emp;//用max() 找salary列中的最大值
+-------------+
| max(salary) |
+-------------+
|    10000.00 |
+-------------+
1 row in set (0.00 sec)
mysql> select min(salary) from emp;//min() 找salary列的最小值
+-------------+
| min(salary) |
+-------------+
|      800.00 |
+-------------+
1 row in set (0.00 sec)
mysql> select sum(salary) from emp;//求salary列的和
+-------------+
| sum(salary) |
+-------------+
|    12600.00 |
+-------------+
1 row in set (0.00 sec)
mysql> select avg(salary) from emp;//求平均
+-------------+
| avg(salary) |
+-------------+
| 4200.000000 |
+-------------+
1 row in set (0.00 sec)

(7)分组查询(当前使用新的emp表)表格如下

mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |    NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 |    NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |    NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)

当需要分组查询时需要使用group  by 子句。

注:凡和聚合函数同时出现的列名,则一定要写在group  by  之后

1.查询每个部门(deptno)和每个部门的工资(sal)和。// select  deptno,sum(sal)from emp(表名)group  by deptno;

mysql> select deptno,sum(sal) from emp group by deptno;/*!按列表的从上往下打印*/
+--------+----------+
| deptno | sum(sal) |
+--------+----------+
|     20 | 10875.00 |
|     30 |  9400.00 |
|     10 |  8750.00 |
+--------+----------+
3 rows in set (0.00 sec)

 2.查询每个部门的部门编号以及每个部门工资大于1500的人数;//select depyno,count(*) from emp where  sal > 1500 group by deptno;

mysql> select deptno,count(*) from emp where sal>1500 group by deptno;
+--------+----------+
| deptno | count(*) |
+--------+----------+
|     30 |        2 |
|     20 |        3 |
|     10 |        2 |
+--------+----------+
3 rows in set (0.00 sec)

3.(having)子句

(1)查询工资总和大于9000的部门编号以及工资和;//select deptno,sum(sal) from emp group  by   deptno  having sum(sal)>9000;

mysql> select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;
+--------+----------+
| deptno | sum(sal) |
+--------+----------+
|     20 | 10875.00 |
|     30 |  9400.00 |
+--------+----------+
2 rows in set (0.00 sec)

注:where和having的区别:

having :是在分组后对数据进行过滤,where是在分组前对数据进行过滤

              having后面可以使用分组函数(统计函数)

where:where是对分组前记录的条件,如果某行记录没有满足where子句的条件,那么这行记录不会参加分组;而having是对分组后数据的约束。

          where后面不可以使用分组函数。

(8)LIMIT

limit用来限定查询结果的起始行,以及总行数。

1.查询emp表5行,起始行从1开始;// select  *   from   emp  limit  1,5;

mysql> select * from emp limit 1,5;/*!查询结果从第二行开始向下打印5行*/
+-------+--------+----------+------+------------+---------+---------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+----------+------+------------+---------+---------+--------+
|  7499 | ALLEN  | SALESMAN | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER  | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER  | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
+-------+--------+----------+------+------------+---------+---------+--------+
5 rows in set (0.00 sec)
/*!起始位置为0*/

2.分页查询

如果一页记录为4行,那么查询第三页// select  *  from  emp  limit  12,4;

mysql> select * from emp limit 12,4;
+-------+--------+---------+------+------------+---------+------+--------+
| empno | ename  | job     | mgr  | hiredate   | sal     | comm | deptno |
+-------+--------+---------+------+------------+---------+------+--------+
|  7902 | FORD   | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL |     20 |
|  7934 | MILLER | CLERK   | 7782 | 1982-01-23 | 1300.00 | NULL |     10 |
+-------+--------+---------+------+------------+---------+------+--------+
2 rows in set (0.00 sec)
/*!因为emp表只有14行,所以第4页只有2行*/

(9) 子查询

一个select语句中包含另一个select语句。

子查询是嵌套查询,就是select中包含select,如果一条语句中存两个,或者两个以上的select,那么就是子查询语句了。

子查询出现的位置;

1.where 后,作为被查询条件的一部分;

2.from后,坐表;

当子查询出现在where后作为条件时,还可以使用(any,all)关键字;

子查询结果集的形式;

a.单行单列(用于条件)

b.单行多列(用于条件)

c.多行单列(用于条件)

例:

工资大于jones的工资,其中jones工资需要一条子查询//select  *  from emp  sal >(select  sal  from emp  where  ename=’jones‘);

mysql> select * from emp where sal>(select sal from emp where ename='jones');
+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job       | mgr  | hiredate   | sal     | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
|  7788 | SCOTT | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL |     20 |
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL |     10 |
|  7902 | FORD  | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL |     20 |
+-------+-------+-----------+------+------------+---------+------+--------+
3 rows in set (0.00 sec)

单行单列(查询jones的工资)//select  sal  from  emp  where  ename='jones';

mysql> select sal from emp where ename='jones';
+---------+
| sal     |
+---------+
| 2975.00 |
+---------+
1 row in set (0.00 sec)

单行多列(查询jones的信息)//select  *  from  emp  where  ename=’jones‘;

mysql> select * from emp where ename='jones';
+-------+-------+---------+------+------------+---------+------+--------+
| empno | ename | job     | mgr  | hiredate   | sal     | comm | deptno |
+-------+-------+---------+------+------------+---------+------+--------+
|  7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL |     20 |
+-------+-------+---------+------+------------+---------+------+--------+
1 row in set (0.00 sec)

多行单列(查询某一列)select  sal  from  emp;

mysql> select sal from emp;
+---------+
| sal     |
+---------+
|  800.00 |
| 1600.00 |
| 1250.00 |
| 2975.00 |
| 1250.00 |
| 2850.00 |
| 2450.00 |
| 3000.00 |
| 5000.00 |
| 1500.00 |
| 1100.00 |
|  950.00 |
| 3000.00 |
| 1300.00 |
+---------+
14 rows in set (0.00 sec)
  • 工资高于30号部门所有人工资,其中30部门的所有人工资是子查询。//select  * from  emp  where sal >(select  sal  from emp where deptno=30);
    mysql> select * from emp where sal>all(select sal from emp where deptno=30);
    +-------+-------+-----------+------+------------+---------+------+--------+
    | empno | ename | job       | mgr  | hiredate   | sal     | comm | deptno |
    +-------+-------+-----------+------+------------+---------+------+--------+
    |  7566 | JONES | MANAGER   | 7839 | 1981-04-02 | 2975.00 | NULL |     20 |
    |  7788 | SCOTT | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL |     20 |
    |  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL |     10 |
    |  7902 | FORD  | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL |     20 |
    +-------+-------+-----------+------+------------+---------+------+--------+
    4 rows in set (0.00 sec)

(10)多表查询

多表查询有以下几种:

1.合并结果集:union、union all

作用:合并结果集就是把两个select语句的查询结果合并在一起;

union : 去除重复记录

union all : 不去除重复记录

注意: 被合并的两个结果的列数和列类型必须相同。

/*!创建两个表book和person,并将这两个表合并*/
mysql> create table book(
    -> id int,
    -> name varchar(10)
    -> );
Query OK, 0 rows affected (0.07 sec)

mysql> insert book(id,name)values(1,'红楼梦'),(2,'三国');
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from book;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 红楼梦 |
|    2 | 三国   |
+------+--------+
2 rows in set (0.00 sec)

mysql> create table person(
    -> id int,
    -> name varchar(10)
    -> );
Query OK, 0 rows affected (0.14 sec)

mysql> insert person(id,name) values(2,'宋江'),(3,'贾宝玉');
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from person;
+------+--------+
| id   | name   |
+------+--------+
|    2 | 宋江   |
|    3 | 贾宝玉 |
+------+--------+
2 rows in set (0.00 sec)
mysql> select *from book union select * from person;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 红楼梦 |
|    2 | 三国   |
|    2 | 宋江   |
|    3 | 贾宝玉 |
+------+--------+
4 rows in set (0.00 sec)

2.连接查询:

等值链接   // select  *  from  表名1 ,表名2   where  表名1.列名  操作符  表名2 .列名;

   (a)内连接  【inter】 join  on 

select  *  from  表名1  inter    join    表名2    on   连接条件 ;  

    (b)外连接  outer   join  on

select  *  from   表名1   outer  join  表名2   on  连接条件  ;

    (c)做外连接  left  outer  join         右外连接  right  outer  join 

select  *   from   表名1  left  outer  join  表名2   on  连接条件 ;

    (d)全连接(mysql不支持)

3.笛卡尔积

select  *  from  表名1,表名2;

4.外键(Foreign Key)约束 (常用“一对多”关联关系)

创建外键方式:在创建表的sql语句中使用:

constraint(约束名称)

foreign  key(充当外键的列名)

references(主表名称)(与子表外键关联的列名)


mysql> create table student(
    ->  id int primary key,
    -> name varchar(10),
    ->  sid int,
    -> foreign key(sid) references school(id));
Query OK, 0 rows affected (0.14 sec)
mysql> insert school (id ,name)values(1,'清华'),(2,'千锋');
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> insert student(id,name,sid)values(1,'张三',2),(2,'李四',1),(3,'王五',2);
Query OK, 3 rows affected (0.08 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> select * from school;
+----+------+
| id | name |
+----+------+
|  1 | 清华 |
|  2 | 千锋 |
+----+------+
2 rows in set (0.00 sec)

mysql> select * from student;
+----+------+------+
| id | name | sid  |
+----+------+------+
|  1 | 张三 |    2 |
|  2 | 李四 |    1 |
|  3 | 王五 |    2 |
+----+------+------+
3 rows in set (0.00 sec)
mysql> select * from school,student;
+----+------+----+------+------+
| id | name | id | name | sid  |
+----+------+----+------+------+
|  1 | 清华 |  1 | 张三 |    2 |
|  2 | 千锋 |  1 | 张三 |    2 |
|  1 | 清华 |  2 | 李四 |    1 |
|  2 | 千锋 |  2 | 李四 |    1 |
|  1 | 清华 |  3 | 王五 |    2 |
|  2 | 千锋 |  3 | 王五 |    2 |
+----+------+----+------+------+
6 rows in set (0.00 sec)

5.字符函数

concat(string1,string2)---连接两个字符串的内容

concat_WS("连接符",string1 ,string2)---使用连接符连接字串内容

upper(string)---将字串转成大写

lower(string)---将字串转成小写

left(string,num)----取一个字符串的前多少位

right(string,num)----取 一个字符串的后多少位

6.实用函数

>1         数值运算符和函数;

ceil()  进一取整

div()   整数除法

floor()  舍一取整

mod() 取余数或取模

power(a,b)  幂运算-----a^b

round()     四舍五入

truncate    数字截取--------------truncate(123.45,-2)---100

7.日期时间函数

now()    当前日期和时间

curdate()   当前日期

curtime()   当前时间

date_add()   日期变化

datediff()   日期差值-----select  datediff(now(),’2013-12-28');

date_format()   日期格式化 

8.加密函数:

MD5()----web加密----------不可逆()

mysql> select md5('123');
+----------------------------------+
| md5('123')                       |
+----------------------------------+
| 202cb962ac59075b964b07152d234b70 |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select md5('202cb962ac59075b964b07152d234b70');
+-----------------------------------------+
| md5('202cb962ac59075b964b07152d234b70') |
+-----------------------------------------+
| d9b1d7db4cd6e70935368a1efb10e377        |
+-----------------------------------------+
1 row in set (0.00 sec)

 password()--------修改用户密码

总结:

查询语句书写顺序:

(select)--from--where--group  by--having--order  by--limit;

查询语句执行顺序:

from--where--group  by--having--(select)--order  by-- limit;

猜你喜欢

转载自blog.csdn.net/Feng0811xin/article/details/84290187