mysql笔记

Posted TrumanPan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql笔记相关的知识,希望对你有一定的参考价值。

MySQL学习笔记

数据库概述

SQL概述

  • SQL,一般发音为 sequel, SQL 的全称 Structured Query Language), SQL 用来和数据库打交道,完成和数据库的通信, SQL是一套标准。 但是每一个数据库都有自己的特性别的数据库没有,当使用这个数据库特性相关的功能,这时 SQL 语句可能就不是标准了.(90%以上的 SQL 都是通用的 )

什么是数据库

  • 数据库,通常是一个或一组文件,保存了一些符合特定规格的数据,数据库对应的英语单词是 DataBase,简称:DB,数据库
    软件称为数据库管理系统( DBMS),全称为 DataBase Management System,如: Oracle、 SQL Server、 MySql、 Sybase、informix、 DB2、 interbase、 PostgreSql 。

MySql 概述

  • MySQL 最初是由“ MySQL AB”公司开发的一套关系型数据库管理系统( RDBMS-Relational Database Mangerment System)。MySQL 不仅是最流行的开源数据库,而且是业界成长最快的数据库,每天有超过 7 万次的下载量,其应用范围从大型企业到专有的嵌入应用系统。
  • MySQL AB 是由两个瑞典人和一个芬兰人: David Axmark、 Allan Larsson 和 Michael “Monty” Widenius 在瑞典创办的。在 2008 年初, Sun Microsystems 收购了 MySQL AB 公司。在 2009 年, Oracle 收购了 Sun 公司,使 MySQL 并入 Oracle 的数据库产品线。

表的理解

  1. 表(table)是什么?
姓名 性别 年龄
张三 20 ----》行(记录)
李四 23 ----》行(记录)
张美丽 21 ----》行(记录)

数据库当中是以表格的形式表示数据的。——表比较直观

  • 行(row):被称为数据/记录
  • 列(column):被称为字段

如:姓名字段、性别字段、年龄字段等

  • 约束:约束有多种,如唯一性约束(添加该约束后,该字段中的数据不能重复)

SQL语句的分类

  • DQL:

    数据查询语言(DQL-Data Query Language)

    代表关键字:select

  • DML:

    数据操纵语言(DML-Data Manipulation Language)

    代表关键字:insert(增),delete(删),update(改)

    主要操作表中的数据

  • DDL:

    数据定义语言(DDL-Data Definition Language)

    代表关键字:create (新建),drop(删除),alter(修改),

    主要操作的是表的结构,不是表的数据。

  • TCL:

    事务控制语言(TCL-Transactional Control Language)

    代表关键字:commit(事物提交) ,rollback(事物回滚)

  • DCL:

    数据控制语言(DCL-Data Control Language)
    代表关键字:grant(授权),revoke(撤销授权)

导入数据

  1. 连接MySQL

      C:\\Users\\18461>mysql -uroot -pWWW.1234
      Welcome to the MySQL monitor.  Commands end with ; or \\g.
      Your MySQL connection id is 2
      Server version: 5.5.36 MySQL Community Server (GPL)
    
      Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
    
      Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.
    
      mysql>
    
  2. 创建“ bjpowernode”数据库

    mysql>create database bjpowernode;
    
  3. 选择数据库

    mysql> use bjpowernode
    
  4. 导入数据

    source C:\\software\\MySQL_databases\\bjpowernode.sql
    
  5. 删除数据库(不要做)

    mysql> drop database bjpowernode;
    

mysql常用命令

查询mysql版本

: select version();

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.36    |
+-----------+
1 row in set (0.00 sec)

查询当前使用的数据库

: select database();

mysql> select database();
+-------------+
| database()  |
+-------------+
| bjpowernode |
+-------------+
1 row in set (0.00 sec)

终止一条语句

如股想终止一条正在输入的语句,可以输入\\c。

mysql> show
    ->
    ->
    ->
    ->
    ->
    ->
    -> \\c

登录mysql

  • 本地登录(显示编写密码的方式)

    MySQL登录方式:mysql -uroot -pWWW.1234

      C:\\Users\\18461>mysql -uroot -pWWW.1234
      Welcome to the MySQL monitor.  Commands end with ; or \\g.
      Your MySQL connection id is 2
      Server version: 5.5.36 MySQL Community Server (GPL)
    
      Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
    
      Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.
    
      mysql>
    
  • 本地登录(隐藏密码的方式)

    MySQL登录方式:mysql -uroot -p

      C:\\Users\\18461>mysql -uroot -p
      Enter password: ********
      Welcome to the MySQL monitor.  Commands end with ; or \\g.
      Your MySQL connection id is 2
      Server version: 5.5.36 MySQL Community Server (GPL)
    
      Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
    
      Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.
    
      mysql>
    

退出mysql

: 可以使用\\q、QUIT(quit)或(EXIT)exit

mysql> exit
Bye

创建数据库

:create database XXX(新建数据库名);

mysql> create database StudentData;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| studentdata        |
| test               |
+--------------------+
5 rows in set (0.00 sec)

查询数据库

:show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

​ mysql默认自带了四个数据库。

使用数据库

:use XXX(数据库名)

mysql> use test
Database changed

​ 表示正在使用一个名字为test的数据库

查询数据库下的表

:show tables;

mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

查询表的数据

:select * from 表名;

mysql> select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME      | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

查询表的结构

:desc 表名; describe(描述;形容;)

mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| EMPNO    | int(4)      | NO   | PRI | NULL    |       |
| ENAME    | varchar(10) | YES  |     | NULL    |       |
| JOB      | varchar(9)  | YES  |     | NULL    |       |
| MGR      | int(4)      | YES  |     | NULL    |       |
| HIREDATE | date        | YES  |     | NULL    |       |
| SAL      | double(7,2) | YES  |     | NULL    |       |
| COMM     | double(7,2) | YES  |     | NULL    |       |
| DEPTNO   | int(2)      | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.01 sec)

简单的查询

查询一个字段

  • select 字段名 from 表名;

    mysql> select dname from dept;
    +------------+
    | dname      |
    +------------+
    | ACCOUNTING |
    | RESEARCH   |
    | SALES      |
    | OPERATIONS |
    +------------+
    4 rows in set (0.00 sec)
    

查询多个字段

  • 使用逗号“,”隔开

    select 字段名1,字段名2 from 表名;

    mysql> select dname, loc from dept;
    +------------+----------+
    | dname      | loc      |
    +------------+----------+
    | ACCOUNTING | NEW YORK |
    | RESEARCH   | DALLAS   |
    | SALES      | CHICAGO  |
    | OPERATIONS | BOSTON   |
    +------------+----------+
    4 rows in set (0.00 sec)
    

查询所有字段

  • 使用星号“ * ”表示字段名

    select * from 表名;

    mysql> select * from dept;
    +--------+------------+----------+
    | DEPTNO | DNAME      | LOC      |
    +--------+------------+----------+
    |     10 | ACCOUNTING | NEW YORK |
    |     20 | RESEARCH   | DALLAS   |
    |     30 | SALES      | CHICAGO  |
    |     40 | OPERATIONS | BOSTON   |
    +--------+------------+----------+
    4 rows in set (0.00 sec)
    

    该方法的缺点:

    1. 效率低
    2. 可读性差

字段的别名

​ select 待起别名字段 as 别名 from 表名;

  • as关键字只作用于其前面的一个标识符。

  • as可以省略,用空格代替,后直接输入字段的别名。

  • as省略时别名中有空格,别名(标识符)用单引号‘ ’/双引号” “括起来。

  • 别名是中文,用单引号括起来。

mysql> select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME      | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

mysql> select DEPTNO, DNAME, LOC as deptLOC from dept;
+--------+------------+----------+
| DEPTNO | DNAME      | deptLOC  |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)
//as可以省略,用空格代替,后直接输入字段的别名。
mysql> select DEPTNO, DNAME deptName, LOC from dept;
+--------+------------+----------+
| DEPTNO | deptName   | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)
//as省略时别名中有空格,别名(标识符)用单引号括起来。
mysql> select DEPTNO, DNAME \'dept Name\', LOC from dept;
+--------+------------+----------+
| DEPTNO | dept Name  | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

查询字段的操作

  • 可以对number型的字段进行加减乘除

  • 不会修改原数据

    mysql> select ename, sal*12 \'年薪\' from emp;
    +--------+----------+
    | ename  | 年薪        |
    +--------+----------+
    | SMITH  |  9600.00 |
    | ALLEN  | 19200.00 |
    | WARD   | 15000.00 |
    | JONES  | 35700.00 |
    | MARTIN | 15000.00 |
    | BLAKE  | 34200.00 |
    | CLARK  | 29400.00 |
    | SCOTT  | 36000.00 |
    | KING   | 60000.00 |
    | TURNER | 18000.00 |
    | ADAMS  | 13200.00 |
    | JAMES  | 11400.00 |
    | FORD   | 36000.00 |
    | MILLER | 15600.00 |
    +--------+----------+
    14 rows in set (0.00 sec)
    

条件查询

语法格式:

select
	字段1, 字段2,字段3...
from
	表名
where
	条件;

支持运算符

运算符 说明
= 等于
<>或!= 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
between…and …. 两个值之间,等同于 >= and <=闭区间
is null 为 null( is not null 不为空)
and 并且
or 或者
in 包含,相当于多个 or ( not in 不在这个范围中)
not not 可以取非,主要用在 is 或 in 中
like like 称为模糊查询,支持%(百分号)或_(下划线)匹配; % 匹配任意多个字符, _ 匹配任意一个字符

用例:

  • 查询工资大于2000的员工编号、姓名、薪资。

    mysql> select empno, ename, sal from emp where sal >= 2000;
    +-------+-------+---------+
    | empno | ename | sal     |
    +-------+-------+---------+
    |  7566 | JONES | 2975.00 |
    |  7698 | BLAKE | 2850.00 |
    |  7782 | CLARK | 2450.00 |
    |  7788 | SCOTT | 3000.00 |
    |  7839 | KING  | 5000.00 |
    |  7902 | FORD  | 3000.00 |
    +-------+-------+---------+
    6 rows in set (0.00 sec)
    
  • 查询工资等于800或5000的员工信息。

    select * from emp where sal in (800, 5000);与select * from emp where sal=800 or sal=5000;等价

    mysql> select * from emp where sal in (800, 5000);
    +-------+-------+-----------+------+------------+---------+------+--------+
    | EMPNO | ENAME | JOB       | MGR  | HIREDATE   | SAL     | COMM | DEPTNO |
    +-------+-------+-----------+------+------------+---------+------+--------+
    |  7369 | SMITH | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL |     20 |
    |  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL |     10 |
    +-------+-------+-----------+------+------------+---------+------+--------+
    2 rows in set (0.00 sec)
    
  • 查询工资大于1250或3000的员工信息。

    • 标识符 between 1250 and 3000;
mysql> select ename, sal from emp where sal between 1250 and 3000;
+--------+---------+
| ename  | sal     |
+--------+---------+
| ALLEN  | 1600.00 |
| WARD   | 1250.00 |
| JONES  | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE  | 2850.00 |
| CLARK  | 2450.00 |
| SCOTT  | 3000.00 |
| TURNER | 1500.00 |
| FORD   | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
10 rows in set (0.00 sec)

模糊查询

  • like 称为模糊查询
    • 支持%(百分号)或_(下划线)匹配
    • % 匹配任意多个字符
    • _ 匹配任意一个字符

用例:

//找出名字中含有o的员工信息
mysql> select * from emp where ename like \'%o%\';
+-------+-------+---------+------+------------+---------+------+--------+
| 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 |
|  7902 | FORD  | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL |     20 |
+-------+-------+---------+------+------------+---------+------+--------+
3 rows in set (0.00 sec)
//找出名字以T结尾的
select ename from emp where ename like \'%t\';
//找出名字以k开头的
select ename from emp where ename like \'k%\';
//找出第二个字母是a的
select ename from emp where ename ike \'_a%\'

找出名字中有下划线的

​ select name from t_student where name ike \'%\\_%\';

\\ 反斜杠是转义字符

排序数据

排序语句总是在最后且最后执行

单一字段排序

  • 排序采用 order by 字段1,字段2... :

    order by 后面跟上排序字段,排序字段可以放多个,多个采用逗号间隔, order by 默认采用升序 asc (降序在标识符后加desc),如果存在 where 子句那么 order by 必须放到 where 语句的后面。

    descent(下降;下倾;斜坡;)

//查询所有员工薪资,排序?
select 
	ename, sal
from 
	emp
order by
	sal;
	
+--------+---------+
| ename  | sal     |
+--------+---------+
| SMITH  |  800.00 |
| JAMES  |  950.00 |
| ADAMS  | 1100.00 |
| WARD   | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN  | 1600.00 |
| CLARK  | 2450.00 |
| BLAKE  | 2850.00 |
| JONES  | 2975.00 |
| FORD   | 3000.00 |
| SCOTT  | 3000.00 |
| KING   | 5000.00 |
+--------+---------+
14 rows in set (0.00 sec)
//找出工资在1250到3000之间的员工信息,薪资降序排列
mysql> select ename, sal from emp where sal between 1250 and 3000 order by sal desc;
+--------+---------+
| ename  | sal     |
+--------+---------+
| FORD   | 3000.00 |
| SCOTT  | 3000.00 |
| JONES  | 2975.00 |
| BLAKE  | 2850.00 |
| CLARK  | 2450.00 |
| ALLEN  | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| MARTIN | 1250.00 |
| WARD   | 1250.00 |
+--------+---------+
10 rows in set (0.00 sec)

主次排序条件

//查询员工姓名和薪资,要求主排序条件:薪资升序
//次排序条件:姓名升序(工资一样时,再进行排序)
select ename, sal from emp order by sal, ename;
+--------+---------+
| ename  | sal     |
+--------+---------+
| SMITH  |  800.00 |
| JAMES  |  950.00 |
| ADAMS  | 1100.00 |
| MARTIN | 1250.00 |
| WARD   | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN  | 1600.00 |
| CLARK  | 2450.00 |
| BLAKE  | 2850.00 |
| JONES  | 2975.00 |
| FORD   | 3000.00 |
| SCOTT  | 3000.00 |
| KING   | 5000.00 |
+--------+---------+
14 rows in set (0.00 sec)

字段位置排序

  • 根据字段的位置进行排序
  • 了解一下,不建议在开发中写,这样不健壮
  • 列的顺序易发生改变
//2表示第二列,对第二列进行排序
mysql> select ename, sal from emp order by 2;
+--------+---------+
| ename  | sal     |
+--------+---------+
| SMITH  |  800.00 |
| JAMES  |  950.00 |
| ADAMS  | 1100.00 |
| WARD   | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN  | 1600.00 |
| CLARK  | 2450.00 |
| BLAKE  | 2850.00 |
| JONES  | 2975.00 |
| FORD   | 3000.00 |
| SCOTT  | 3000.00 |
| KING   | 5000.00 |
+--------+---------+
14 rows in set (0.00 sec)

数据处理函数

:又叫单行处理函数

特点:一个输入对应一个输出

函数名 功能
Lower 转换小写
upper 转换大写
substr 取子串( substr(被截取的字符串,起始下标, 截取的长度)
length 取长度
trim 去空格
str_to_date 将字符串转换成日期
date_format 格式化日期
format 设置千分位
round 四舍五入
rand() 生成随机数
Ifnull 可以将 null 转换成一个具体值
concat 字符串的拼接

lower/upper函数

lower(标识名)/upper(标识名)

//lower函数
mysql> select lower(ename) ename from emp;
+--------+
| ename  |
+--------+
| smith  |
| allen  |
| miller |
+--------+
14 rows in set (0.00 sec)

//upper函数
mysql> select upper(ENAME) ENAME from emp;
+--------+
| ENAME  |
+--------+
| SMITH  |
| ALLEN  |
| MILLER |
+--------+
14 rows in set (0.00 sec)

substr函数

截取字符串,substr(被截取的字符串,起始下标, 截取的长度)

mysql> select substr(ename,1,1) as enamefirst from emp;
+------------+
| enamefirst |
+------------+
| S          |
| A          |
| W          |
| A          |
| J          |
| F          |
| M          |
+------------+
14 rows in set (0.00 sec)
//找出员工名字第一个字母是A的员工信息
mysql> select * from emp where substr(ename,1,1) = \'a\';
+-------+-------+----------+------+------------+---------+--------+--------+
| EMPNO | ENAME | JOB      | MGR  | HIREDATE   | SAL     | COMM   | DEPTNO |
+-------+-------+----------+------+------------+---------+--------+--------+
|  7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 |     30 |
|  7876 | ADAMS | CLERK    | 7788 | 1987-05-23 | 1100.00 |   NULL |     20 |
+-------+-------+----------+------+------------+---------+--------+--------+
2 rows in set (0.00 sec)

concat函数

进行字符串的拼接。

mysql> select concat(ename, empno) from emp;
+----------------------+
| concat(ename, empno) |
+----------------------+
| SMITH7369            |
| ALLEN7499            |
| WARD7521             |
| JONES7566            |
| MARTIN7654           |
| BLAKE7698            |
| CLARK7782            |
| SCOTT7788            |
| KING7839             |
| TURNER7844           |
| ADAMS7876            |
| JAMES7900            |
| FORD7902             |
| MILLER7934           |
+----------------------+
14 rows in set (0.00 sec)

length函数

:取长度

mysql> select length(ename) as enameLength from emp;
+-------------+
| enameLength |
+-------------+
|           5 |
|           5 |
|           4 |
|           5 |
|           6 |
|           5 |
|           5 |
|           5 |
|           4 |
|           6 |
|           5 |
|           5 |
|           4 |
|           6 |
+-------------+
14 rows in set (0.00 sec)

trim函数

:去除字符串两边的空格

  • ltrim函数:去除字符串左边空格。
  • rtrim函数:去除字符串右边空格。
//ltrim函数:去除字符串左边空格。
mysql> select * from emp where ename = ltrim(\'   KING\');
+-------+-------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB       | MGR  | HIREDATE   | SAL     | COMM | DEPTNO |
+-------+-------+-----------+------+------------+---------+------+--------+
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL |     10 |
+-------+-------+-----------+------+------------+---------+------+--------+
1 row in set (0.00 sec)

//rtrim函数:去除字符串右边空格。
mysql> select * from emp where ename = ltrim(\'KING     \');
+-------+-------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB       | MGR  | HIREDATE   | SAL     | COMM | DEPTNO |
+-------+-------+-----------+------+------------+---------+------+--------+
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL |     10 |
+-------+-------+-----------+------+------------+---------+------+--------+
1 row in set (0.00 sec)

//trim函数:去除字符串两边的空格
mysql> select * from emp where ename = ltrim(\'              KING     \');
+-------+-------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB       | MGR  | HIREDATE   | SAL     | COMM | DEPTNO |
+-------+-------+-----------+------+------------+---------+------+--------+
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL |     10 |
+-------+-------+-----------+------+------------+---------+------+--------+
1 row in set (0.00 sec)

mysql>

str_to_date函数

将字符串转换成日期

date_format函数

格式化日期

format函数

设置千分位

类switch函数

结构:case... when... then... when... then... else... end

mysql> select
    -> ename,
    -> job,
    -> sal as oldsal,
    -> (case job when \'manager\' then sal*1.1 when \'salesman\' then sal*1.5 else sal end) as newsal
    -> from
    -> emp;
+--------+-----------+---------+---------+
| ename  | job       | oldsal  | newsal  |
+--------+-----------+---------+---------+
| SMITH  | CLERK     |  800.00 |  800.00 |
| ALLEN  | SALESMAN  | 1600.00 | 2400.00 |
| WARD   | SALESMAN  | 1250.00 | 1875.00 |
| JONES  | MANAGER   | 2975.00 | 3272.50 |
| MARTIN | SALESMAN  | 1250.00 | 1875.00 |
| BLAKE  | MANAGER   | 2850.00 | 3135.00 |
| CLARK  | MANAGER   | 2450.00 | 2695.00 |
| SCOTT  | ANALYST   | 3000.00 | 3000.00 |
| KING   | PRESIDENT | 5000.00 | 5000.00 |
| TURNER | SALESMAN  | 1500.00 | 2250.00 |
| ADAMS  | CLERK     | 1100.00 | 1100.00 |
| JAMES  | CLERK     |  950.00 |  950.00 |
| FORD   | ANALYST   | 3000.00 | 3000.00 |
| MILLER | CLERK     | 1300.00 | 1300.00 |
+--------+-----------+---------+---------+
14 rows in set (0.00 sec)

round函数

功能:四舍五入

格式:round(小数, 小数点后保留位数)

范例:round(123.456, 2)--> 结果等于123.46

select 字段/数据 from 表名;

  • select后面可以跟某个表的字段名(可以等同看做变量名),也可以跟字面量/字面值(数据)。
//select 后跟数据

mysql> select \'abc\' as str from emp;
+-----+
| str |
+-----+
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
| abc |
+-----+
14 rows in set (0.00 sec)

mysql> select round(123.456,2) as str from emp;
+--------+
| str    |
+--------+
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
+--------+
14 rows in set (0.00 sec)

rand()函数

生成随机数

//生成随机数
mysql> select rand() from emp;
+-----------------------+
| rand()                |
+-----------------------+
| 0.0036685355041814367 |
|     0.252189609456984 |
|    0.2499424715060205 |
|   0.49314355989279557 |
|    0.7158904156795614 |
|    0.1000214294530651 |
|   0.35243593096028636 |
|   0.46211539717588146 |
|    0.2532692237321932 |
|    0.8799999578669667 |
|    0.6401921488718988 |
|    0.5609609014922389 |
|    0.8842280915791431 |
|    0.7382577841526435 |
+-----------------------+
14 rows in set (0.00 sec)

//生成一百以内的随机数
mysql> select round(rand()*100,0) from emp;
+---------------------+
| round(rand()*100,0) |
+---------------------+
|                   4 |
|                  98 |
|                  78 |
|                  94 |
|                  39 |
|                  10 |
|                  35 |
|                  46 |
|                  22 |
|                  73 |
|                  99 |
|                  76 |
|                  84 |
|                  90 |
+---------------------+
14 rows in set (0.00 sec)

Ifnull函数

可以将 null 转换成一个具体值

格式:ifnull(数据,数据为null时指定的值)

//为指定null为何值时,其它数据与null运算
mysql> select ename, (comm+sal)*12 as yearSal from emp;
+--------+----------+
| ename  | yearSal  |
+--------+----------+
| SMITH  |     NULL |
| ALLEN  | 22800.00 |
| WARD   | 21000.00 |
| JONES  |     NULL |
| MARTIN | 31800.00 |
| BLAKE  |     NULL |
| CLARK  |     NULL |
| SCOTT  |     NULL |
| KING   |     NULL |
| TURNER | 18000.00 |
| ADAMS  |     NULL |
| JAMES  |     NULL |
| FORD   |     NULL |
| MILLER |     NULL |
+--------+----------+
14 rows in set (0.00 sec)
//为指定null为0时,其它数据与null运算
mysql> select ename, (ifnull(comm,0)+sal)*12 as yearSal from emp;
+--------+----------+
| ename  | yearSal  |
+--------+----------+
| SMITH  |  9600.00 |
| ALLEN  | 22800.00 |
| WARD   | 21000.00 |
| JONES  | 35700.00 |
| MARTIN | 31800.00 |
| BLAKE  | 34200.00 |
| CLARK  | 29400.00 |
| SCOTT  | 36000.00 |
| KING   | 60000.00 |
| TURNER | 18000.00 |
| ADAMS  | 13200.00 |
| JAMES  | 11400.00 |
| FORD   | 36000.00 |
| MILLER | 15600.00 |
+--------+----------+
14 rows in set (0.00 sec)

分组函数

又叫:聚合函数/多行处理函数

特点:输入多行,最终输出一行。

select count(*) from emp where xxx;

  • 符合条件的所有记录总数。

select count(comm) from emp;

  • comm 这个字段中不为空的元素总数。
count 取得记录数
sum 求和
avg 取平均
max 取最大的数
min 取最小的数

count函数

//计算员工总数量
mysql> select count(ename) from emp;
+--------------+
| count(ename) |
+--------------+
|           14 |
+--------------+
1 row in set (0.00 sec)

//工资大于1500的员工个数
mysql> select count(*) from emp where sal>1500;
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

max/min/sum/avg函数

//找出最高工资
mysql> select max(sal) from emp;
+----------+
| max(sal) |
+----------+
|  5000.00 |
+----------+
1 row in set (0.00 sec)

//找出最低工资
mysql> select min(sal) from emp;
+----------+
| min(sal) |
+----------+
|   800.00 |
+----------+
1 row in set (0.00 sec)

//计算工资和
mysql> select sum(sal) from emp;
+----------+
| sum(sal) |
+----------+
| 29025.00 |
+----------+
1 row in set (0.00 sec)

//计算平均工资
mysql> select round(avg(sal),2) from emp;
+-------------------+
| round(avg(sal),2) |
+-------------------+
|           2073.21 |
+-------------------+
1 row in set (0.00 sec)

分组查询

  • 什么是分组查询?

    在实际应用中,可能需要先分组后对每一组的数据进行操作

格式:

select 
	...
from
	...
group by
	...

group by 函数

//计算每一个部门的工资和?

//计算每个工作岗位的平均薪资?

以上是关于mysql笔记的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 笔记

MySQL学习笔记六

MySQL学习笔记之六:MySQL日志

mysql 优化笔记

Mysql高性能优化笔记(含578页笔记PDF文档),收藏了

mysql个人散乱笔记,慎重参考