Mysql语句:查询

目录

DQL语言:(查询)

起别名:

去重(DISTINCT关键字)

mysql中的+号(运算符)

拼接字段(concat关键字)

条件查询

逻辑查询

like:模糊查询

BETWEEN AND模糊查询

in模糊查询

is,is not和安全等于<=>

ifnull和isnull的区别

排序查询


DQL语言:(查询)

基础查询:select 查询列表 from 表名

查询列表可以是:

  1. 表中的字段、常量值、表达式、函数; 
  2. 查询的结果是一个虚拟的表格;

使用库        use employees;

查询表中单个字段                                   

                                                                select last_name from employees;

查询表中多个字段(不同字段之间逗号隔开)        

                                                                select last_name,salary,email from employees;

查询表中所有字段(*代表所有字段,顺序和原表一样)        

                                                                select * from employees;

查询常量值                                              select 100;                 select 'john';

查询表达式                                              select 100%98;

查询函数                                                  select VERSION();

起别名:

  1. 提高可读性,便于理解
  2. 如果要查询的字段有重名情况用别名可区分

as关键字

            select 100%98 AS 结果;

            select last_name AS 姓,first_name AS 名 from employees;

空格

            select last_name 姓,first_name 名 from employees;

案例:查询salary,显示结果为out put

select salary as "out put" from employees;

去重(DISTINCT关键字)

案例:查询员工表中涉及到的所有部门编号

        select department_id from employees;

        select DISTINCT department_id from employees;

mysql中的+号(运算符)

两个数都为数值型,则做加法运算

            select 100+90;

其中一方为字符型,试图将字符型转换为数值型,成功则继续加法运算;失败则将 字符型数值转换成0

            select '123'+90;

            select 'join'+90;

其中一方为null,结果必为null

            select null+0;

拼接字段(concat关键字)

        select concat('a','b','c') as 结果;

        select concat(last_name,first_name) as 姓名 from employees;

条件查询

语法:select 查询列表 from 表名 where 筛选条件; 
    1.按条件表达式筛选:> < = != <> >= <=
    2.按逻辑表达式筛选 逻辑运算符 && || !    and or not
    3.模糊查询 like、between and、in、is null

select * from employees where salary>12000;
select last_name,department_id from employees where department_id!=90;
select last_name,department_id from employees where department_id<>90;

逻辑查询

--查询工资在10000到20000之间的员工名、工资及奖金 
select last_name,salary,commission_pct from employees where salary>=10000 and salary<=20000;

--查询部门编号不是在90到110之间,或者工资高于15000的员工信息
select * from employees where department_id<90 or department_id>110 or salary>15000; 
select * from employees where not(department_id>=90 and department_id<=110) or salary>15000; 
    

like:模糊查询

        一般搭配通配符使用,可以判断字符型或数值型
            通配符:%        代表任意个字符,包括0个
                            _        代表一个字符

--查询员工名中包含字符a的员工信息
select * from employees where last_name like '%a%';

--查询员工名第二个字符为_的员工名(ESCAPE转义字符关键字)
select last_name from employees where last_name like '_$_%' ESCAPE '$';

BETWEEN AND模糊查询

         包含临界值,大于等于左边小于等于右边

--查询员工编号在100到120之间的员工信息
select * from employees where employee_id >= 100 and employee_id <= 120; 
select * from employees where employee_id BETWEEN 100 and 120; 

in模糊查询

in 判断某字段的值是否属于in列表中的某一项(in列表的值类型必须统一或兼容)

--查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个原告名和工种编号
select last_name,job_id from employees where job_id = 'IT_PROG' or job_id = 'AD_VP' or job_id = 'AD_PRES';


select last_name,job_id from employees where job_id in('IT_PROG','AD_VP','AD_PRES');

is,is not和安全等于<=>

--查询没有奖金的员工名和奖金率(=或<>不能用来判断null值)
select last_name,commission_pct from employees where commission_pct IS NULL; 
--查询有奖金的员工名和奖金率
select last_name,commission_pct from employees where commission_pct IS NOT NULL; 

--安全等于<=> 
select last_name,commission_pct from employees where commission_pct <=> NULL;
select last_name,salary from employees where salary <=> 12000;
--IS NULL仅仅判断null值,<=>既可以判断null值,又能判断普通数值但可读性较低

--查询没有奖金且工资小于18000的salary,last_name
select last_name,salary from employees where commission_pct IS NULL AND salary >= 18000; 
--查询employees表中,job_id不为'IT'或者工资为12000的员工信息
select job_id,salary from employees where job_id <> 'IT' or salary = 12000;

ifnull和isnull的区别

--ifnull函数:判断某字段或表达式是否为null,如果为null则返回指定的值,否则返回原值
select ifnull(commission_pct,0) from employees;

--isnull函数:判断某字段或表达式是否为null,如果是则返回1,否则返回0 
select isnull(commission_pct),commission_pct from employees;

排序查询

select 查询列表 from 表 [where 筛选条件] order by 排序列表 asc(升序,可省)/desc(降序);
--案例:查询员工信息,按工资从高到低排序
select * from employees order by salary desc;
select * from employees order by salary asc;
select * from employees order by salary;
--案例:查询部门编号>=90的员工信息,按入职时间的先后进行排序
select * from employees where department_id >= 90 ORDER BY hiredate ASC;
--案例:按年薪的高低显示员工信息和年薪(按表达式排序)
select *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 from employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
--案例:按年薪的高低显示员工信息和年薪(按别名排序)
select *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 from employees ORDER BY 年薪 DESC;
--案例:按姓名的长度显示员工的姓名和工资(按函数排序,LENGTH(str)获取字符字节长度) 
select LENGTH(last_name) 字节长度,last_name,salary from employees ORDER BY LENGTH(last_name) DESC;
--查询员工信息,要求先按工资升序,再按员工编号降序(按多个字段排序) 
select * from employees ORDER BY salary ASC,employee_id DESC;

你可能感兴趣的:(mysql,数据库,sql)