Sql语句

1.简单查询 (英文标点)

查询所有 * 通配符

select     *       from    employees;
查询关键字   -- * 通配符代表所有(列) --     数据来源 关键字   --    表名
注意: * 不如 所有的列名 ----- 执行过程中 将 * 转化成 所有的列名    开发不建议 *

指定列查询

select 列名,列名 from 表明

简单运算 */±

 select employee_id*10 from employees;

别名 关键字 as 可以省略

select name as cc from user_tb
select 列明 as 别名 (不准中文) from 表名
select 列明 别名 from 表名

合并列 || 对于展示结果的合并

select first_name || last_name as name from employees;` 

distinct 去重

--部门编号   department_id
-查询所有的部门编号
select distinct department_id from employees;

2.有条件的查询数据

关键字 where

 select   结果(列)  from  表  where 条件
    2.1 比较运算  > < >= <= = != 
        select * from employees
        where salary != 17000;
    2.2 逻辑运算  and  or  not
        -- 工资 17000 和 24000
        select * from employees where salary = 17000 or salary = 24000;
        select * from employees where  not salary = 24000;
    2.3 谓词
        1. 枚举   列举所有的内容    in  、 not in
            -- in   查询工资 24000 17000 12000
            select * from employees where salary in(24000,17000,12000);
        2.区间 (闭) 包含 相当于 >= <=  
            between ... and ...   /  not between ... and  取反
       select * from employees where salary not between 8000 and 12000;


    3. like  像  模糊查询            字符类型 需要使用 '字符'
            --like   % 代表多位 不确定    _  代表一位
            select * from employees
            where first_name like '_a___';

    4. 空
            --空   is null  /   is  not null     0 是数据   空 null 没有
            select * from employees
            where department_id is not null;

3.有序(排序) 关键字 order by

注意: 对于查询结果进行排序 (指定排序规则 使用哪一列的数据进行排序)
        --排序  默认升序 asc    手动降序 desc;
        select * from employees
        where salary >5000
        order by salary desc;
        
        select * from employees
        order by hire_date desc,salary desc;   写在前的列会优先进行排序。

4. 函数

4.1内置函数

1.系统当前时间   sysdate()

    --函数   数据来源于表  内置数据
        select sysdate() from employees;
    --虚表  哑表    满足SQL语句的语法规则   dual  一行一列
        select sysdate() from dual;
        -- 年月日 时分秒 星期

        select * from dual; 一行一列
      获得当前日期+时间(date + time)函数:now()也可以
sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值
MySQL通过sql语句获取当前日期|时间|时间戳
https://blog.csdn.net/tanga842428/article/details/52788757

2.to_char(日期,'日期格式')  输出的日期进行格式规范   应用在数据的展示
        -- yyyy-mm-dd   HH:mi:ss  day 星期   仅限数据库
        select to_char(sysdate,'yyyy-mm-dd   HH:mi:ss  day') as shijian from dual;

3.to_date(字符串,格式)  将字符串 转换成 日期格式    数据的存储
        注意: 格式 是字符串中的格式

        -- 2000-01-01     yyyy-mm-dd  将字符--->时间格式  
        -- 展示结果是数据库中的格式   跟你的转换格式没有任何关系
        select to_date('2000-01-01','yyyy-mm-dd') as shijian from dual;

4.2 组函数

组: 针对一组数据(列中的多个数据)的操作。          
    min(组数据)
    max()
    sum()
    avg()
    count()
    length()
    lengthb() 

    select * from employees;
    select min(salary) from employees;
    select max(salary) from employees;
    select avg(salary) from employees;
    select sum(salary) from employees;
    --有效数据条数  不包含 空 null   主键列
    select count(*) from employees; 

4.3 case 常见场景 对右多个结果的结果集进行统计

case具有两种格式。简单case函数和case搜索函数。
--简单case函数
case sex
  when '1' then '男'
  when '2' then '女’
  else '其他' end
--case搜索函数
case when sex = '1' then '男'
     when sex = '2' then '女'
     else '其他' end  
实际例子
一张表数据如下
 1900-1-1 胜
 1900-1-1 胜
 1900-1-1 负
 1900-1-2 胜
 1900-1-2 胜
 写出一条SQL语句,使检索结果如下:
          胜  负
 1900-1-1 2   1
 1900-1-2 2   0
我随手建了这样一个表:

create table test(Date varchar(50) null, Result varchar(50) null)
 

并将上面的数据都插入到表中。

经过一番尝试和修改,终于得到了答案:

select distinct Date,
sum(case Result when '胜' then 1 else 0 end) as '胜',
sum(case Result when '负' then 1 else 0 end) as '负'
from test
group by date

这个语句的意思是 对于胜利的 表示为 1  然后sum  得到结果集 然后别名为 胜

5.分组 group by + 分组条件(列,函数)

--查询每个部门的最大工资  ---按照部门对数据进行分组
    select department_id,max(salary) from employees
    group by department_id;

    --查询1997年 每个月入职人数 
        -- where 条件  1997 年                                     where to_char(hire_date,'yyyy') = 1997
        -- 入职人数 count(*)      group by 按月分组                 group by to_char(hire_date,'mm')
        -- 按照月份排序  order by                                   order by to_char(hire_date,'mm');


       select to_char(hire_date,'mm')as yuefen,count(*) as renshu from employees
       where to_char(hire_date,'yyyy') = 1997
       group by to_char(hire_date,'mm')
       order by to_char(hire_date,'mm');

    注意:
       1.只有出现在 group by 之后的列才可以出现在   select 关键字之后 ---- 错误:不是group by 表达式
          不允许一行一列中的数据出现多种选择

       2.允许在select之后使用组函数(单值结果,不存在多个数据冲突)

      3.如果gruop by之后使用了内置函数  也是可以书写在select关键字之后的。

having 分组之后的条件再判断

平均工资大于5000的部门
 select department_id,avg(salary) from employees
group by department_id
 having avg(salary)>5000;

 注意: 如果判断依据中出现组函数应用,需要使用having,其他情况用where。

 --查询 1997 年 每个月入职员工数大于 2 人的 月份以及人数按照人数降序排序。
     1. 查询1997年
      2. 按照月份分组
      3. count(*) >2
      4. 降序排序
   select count(*),to_char(hire_date,'mm')
   from employees
   where to_char(hire_date,'yyyy')=1997
   group by to_char(hire_date,'mm')
   having count(*) >2
   order by count(*) desc;

关键字顺序 — 单表数据查询操作

select  列名  from  表名(表连接)   where 条件    group by  分组  having  组函数判断   order by 排序

6. 子查询

一条SQL语句中去嵌套另外一条SQL语句 (方便书写)
where子查询

-- where 单值子查询    一条SQL语句的结果是另一条SQL语句的执行条件
where = (SQL语句)

    select department_name from departments
    where department_id = (select department_id from employees
                        where employee_id = 120);

-- 查询员工名称为Steven 的员工的部门名称

-- where 的多值 子查询
where in (SQL语句)
    select department_name from departments
    	where department_id in (select department_id from employees
    	where first_name = 'Steven');

from子查询 对数据进行预处理操作

-- SQL语句的执行结果本身也是一张表                     
 -- from 子查询      另一条SQL语句的执行结果 充当一张表的存在
 -- select * from (SQL语句)
    select first_name || last_name as name from employees;
    --StevenKing
    select name 
    from (select first_name || last_name as name from employees)
    where name = 'StevenKing';

7. 表连接

主键列 : 唯一标识一行数据。 (只有一列)
1.唯一:数据不能重复
2.非空
3.数据类型:无任何要求

外键列 : 表示多张表之间的关联关系

表连接关系 表与表之间的相同的数据的列

表连接: 数据展示有可能涉及多张表的数据级联展示

内连接

1 . inner join 表名 on 连接条件(外键关系)
将两张表中有关联的数据进行连接展示
展示两张表都符合条件的数据

select e.*,d.department_name from employees e
    inner join departments d
    on e.department_id = d.department_id;

外连接 (outer 可以省略)

外连接:左外连和右外连
左右是相对的概念(没有本质的区别)

1.左外连接 left (outer) join

   -- 左外连接  将左边表的数据全部展示,右表中的数据可以连接的进行连接展示。
    select e.employee_id,e.first_name,e.department_id,d.department_id,d.department_name 
    from employees e
    left join departments d
    on e.department_id = d.department_id;

2.右外连接 right(outer) join

-- 右外连接  将右边表的数据全部展示,左表中的数据可以连接的进行连接展示。
select e.employee_id,e.first_name,e.department_id,d.department_id,d.department_name 
from employees e
right join departments d
on e.department_id = d.department_id;

3.全外连接 (了解) full(outer) join

   -- 全外连接  所有数据
    select e.employee_id,e.first_name,e.department_id,d.department_id,d.department_name 
    from employees e
    full join departments d
    on e.department_id = d.department_id;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值