1.mysql初始

  • 启动/关闭mysql服务

    $ net stop mysql 
    $ net start mysql 
  • 服务器端登录

    $ mysql -h localhost -P 3306 -u root -p

2.mysql常见命令

  • 查看当前所有数据库

    $ show databases;
  • 打开指定的库

    $ use 库名;
  • 查看其他库的所有表

    $ show tables from ku mi
  • 创建表

    $ create table 表名(
    列名 列类型;
    列名 列类型;
    。。。
    );
  • 查看表结构

    $ desc 表名;
  • 查看服务器的版本

    # 方式一:登录到mysql服务端
    $ select version()
    #方式二:命令行
    $ mysql --version 
    or 
    $ mysql --V

3.select用法说明

  • 使用别名

    #使用AS来指定别名
    $ select last_name AS 姓,first_name AS 名 from employees; 
    #如果别名中有特殊字符,则需使用双引号修饰
    $ select 100%98 AS "out put";
  • 去重

    #使用distinct
    $ select distinct department_id from employees;
  • mysql中+号的使用

    # 仅有一个功能:运算符
    $ select 100+90;     #两个操作符均为数值型,则做加法运算
    $ select '123'+90;   #其中一方为字符型,试图将字符型数值转换成数值型
                       如果转换成功,则继续做加法运算
    $ select 'join'+10;  #如果转换失败,则将字符型转换成0,再加法
    $ select null+10;    #只要其中一方为null,则结果肯定为null
    $ select employee_id, last_name, salary*12 AS "Annual salary" from employees;
  • 使用concat函数来拼接字符串

    $ select concat('a','b','c') AS 结果;
    $ select concat(last_name, first_name) AS 姓名 from employees;
    #并且如果其中有一个字段为null,则拼接后的字符串也为null
    $ select concat(last_name,',', first_name, null) AS 姓名 from employees;
  • 判断字段是否为null

    #如果奖金率为null,则显示0
    #使用IFNULL函数:判断某字段或表达式是否null,如果为null,返回指定的值,否则返回原本的值
    #下面的解释:如果commission_pct为null则返回0
    $ select IFNULL(commission_pct,0) AS 奖金率,commission_pct from employees;
    #使用ISNULL函数:判断某字段或表达式是否为null,如果是返回1,否则返回0
    #下面的解释:如果commission_pct为null则返回1,表示true
    select ISNULL(commission_pct,0),commission_pct from employees;
  • 模糊查询中的通配符、以及对特殊字符进行转义

    #like一般和通配符搭配使用,可以判断字符型或数值型
    # %:表示任意多个字符,包含0个字符
    # _:下划线表示一个字符
    $ SELECT last_name, salary FROM employees WHERE last_name LIKE '__n_l%';
    # 对下划线_进行转义,\表示对后面的字符进行转义
    $ SELECT last_name, salary FROM employees WHERE last_name LIKE '_\_%';
    # 同样也可以使用 escape来自定义字符以表示转义说明
    $ SELECT last_name, salary FROM employees WHERE last_name LIKE '_$_%' ESCAPE '$';
    #补充说明:
    #试问:select * from employees;
    #和 select * from employees where commission_pct like '%%' and last_name like '%%';是否一样
    #回答:可能不一样!,如果判断的字段有null值的情况下,前者全部查询出来,后者只查询出来不为null的。

    4.单行函数

  • trim函数使用

    #去字符串前后空格
    $ select trim('    bea    ') AS out_put;
    #去字符串前后指定字符
    $ select trim('a' from 'aaaabeautifulaaaaa');
    #返回beautiful
  • lpad和rpad函数使用

    # lpad 用指定字符实现左填充指定长度
    $ select lapd('snow',6,'*') AS output;
    #输出snow**
    $ select rpad('snowboard',4,'@') AS output;
    输出snow,即会截断
  • 日期函数

  • str_to_date将字符通过指定的格式转换成日期
    $ select str_to_date('1996--9-29','%Y--%c-%d') AS 日期
    # 输出1996-09-29
    #具体例子查询入职日期为1992-4-3的员工信息
    $ select * from employees where hiredate = str_to_date('4-3 1992',''%c-%d %Y');
    #即str_to_date函数会先将'4-3 1992'按照格式解析为规范日期
  • date_format 将日期按照指定格式转换成字符
    $ select date_format(now(),'%y年%m月%d日');
    #输出21年01月17日
    #
    #例子:查询有奖金的员工姓名和入职日期(xx月/xx日 xx年)
    $ select last_name, date_format(hiredate,'%m月/%d日 %y年') AS 入职日期 from employees where commission_pct is not null

    5.分组函数group by

  • 添加分组的筛选条件

    #如查询邮箱中包含a字符的,每个部门的平均工资
    $ select AVG(salary),department_id
    from employees
    where email like '%a%'
    group by department_id;
  • 添加分组的筛选条件

#查询那个部门的员工个数>2
#第一步:查询每个部分的员工个数
$ select count(*), department_id
 from employees
 group by department_id;
#第二步:根据第一步结果进行筛选,查询那个部门的员工个数大于2
$ select count(*), department_id
$ from employees
$ group by department_id
**having count(*)>2**;
  • 按多个字段分组

    #案例:查询每个部门每个工种的员工的平均工资
    $ select AVG(salary),department_id, job_id
    from employees
    group by department_id, job_id;
    #再排序一下,按照平均工资的降序
    $ select AVG(salary),department_id, job_id
    from employees
    group by department_id, job_id
    order by AVG(salary) DESC;

    6.多表查询

  • 自连接

    #案例:查询员工姓名,以及其上级名称
    $ select e.employee_id, e.last_name, m.employee_id, m.last_name
    from employees e, employees m
    where e.manager_id = m.employee_id;