第二章 MySQL-DQL命令

2023-03-30  本文已影响0人  梅梅golang

IFNULL()函数的用法:

判断某一列是否为null,第一个参数数目标列,如果是null,那么你想让他返回输出什么内容就传二个参数!

select IFNULL(commission_pct,0) as "结果" from 
多个字段查询也可以嵌套如下:
select concat(first_name,",",last_name,",",INFNUL(commission_pct,0)) as "结果" 
from employees;

条件查询where关键字

  1. 格式:select 查询列表 from 表名 where 筛选条件;
  2. 按条件表达式筛选:
    1. 按条件表达式筛选
    2. 小与符号 < 等于符号 = 不等于符号 != 不等于符号 <>
select * from employees where salary > 12000;
select first_name, department_id from employees where department_id != 90;
select
       first_name,salary,commission_pct
from   
       employees
where  
       salary >= 10000 and salary <= 20000;
select 
      * 
from 
      employees 
where 
      department_id < 90 or department_id > 110 or salary > 15000;
或者这样写:用not关键字,not关键字就是java中取反逻辑表达式:
select 
       * 
from 
       employees 
where 
       not (department_id>=90 and department_id<=110) or salary >15000;

模糊查询 Like的用法

模糊查询关键字有以下几个:
Like, between and, in, is null, is not null,

select * from employees where first_name like '%a%';
select 
      last_name,salary 
from  
      employees 
where 
      last_name like '__n_l%';
这种写法是用转义符来区分
select
    last_name
from
    employees
where
    last_name like '_\_%';
或者用关键字escape来指定转义符,这样的话你的转义符就可以随意定义成$或者#等各种不一样的符号,
都他认为是转移符:
select
    last_name
from
    employees
where
    last_name like '_$_%' escape '$'

between and 的用法

select * from employees where employee_id between 100 and 120;

In()的用法

写法一:
select
       last_name,job_id
from
     employees
where job_id = 'IT_PROT' OR job_id = 'AD_VP' OR job_id = 'AD_PRES';

写法二:(这里使用的是in关键字)
select
       last_name,job_id
from
       employees
where job_id in('IT_PROT','AD_VP','AD_PRES');

总结:in关键字主要是判断某个字段的值是否属于in列表中的某一项

is null 用法

select 
       first_name,commission_pct 
from 
       employees 
where 
      commission_pct is null;
select 
      first_name,commission_pct
from  
      employees
where 
      commission_pct is not null ;

以下是练习题

select
       concat(last_name,first_name) as "姓名",
       department_id as "部门号",
       salary*12*(1+ifnull(commission_pct,0)) as "年薪"
from   
       employees
where
      employee_id = 176;
注意:年薪 = 月薪*12+(1+奖金率),而奖金率字段有可能会null所以用上了ifnull()函数
select 
        last_name,salary
from  
        employees
where  
        not salary between 5000 and 12000 
order by salary asc ;
select 
        last_name,department_id
from    
        employees
where   
        department_id in (50,20);
select  
        last_name,job_id
from    
        employees
where 
        manager_id is null;
select 
        last_name,salary,commission_pct
from    
        employees
where   
        commission_pct is not null ;
select 
        last_name
from    
        employees
where   
        last_name like '__a%'
select
       last_name
from
       employees
where
       last_name like '%ae%';
select *
from
       employees
where  
       first_name like '%e'
select 
       last_name,job_id
from
       employees
where 
       department_id between 80 and 100;
select
       last_name,job_id
from
       employees
where  
       manager_id in (100,101,110);

以上内容较多,需要静心新来多敲几遍,方可记住

上一篇下一篇

猜你喜欢

热点阅读