MySql数据库查询(DQL)语言—常用查询

    xiaoxiao2022-07-15  154

    查询基本概念

    SQL(Structure Query Language)语言是数据库的核心语言,DQL(Data Query Language) 数据查询语言基本结构是由SELECT子句,FROM子句,WHERE子句组成的查询块:

    SELECT <字段名表> FROM <表或视图名> WHERE <查询条件>

    而查询的分类为:

    基础查询条件查询排序查询常见函数分组函数分组查询连接查询子查询分页查询union联合查询

    基础查询

    查看departments表结构 desc departments;查看当前字符集格式 show variables like '%char%';查询单个字段 select last_name from employees;查询多个字段 select last_name, salary from employees;查询常量 select 100; select 'tom';查询表达式 select 100*98;查询函数 select VERSION();起别名查询 select last_name as 姓,first_name as 名 from employees; 或者 select last_name 姓,first_name 名 from employees;查询工资并以out put为别名 select salary as "out put" from employees;去重复查询departments表中涉及到了哪些位置id select distinct loaction_id from departments;使用concat查询员工名和姓并取别名为姓名 select concat(last_name,first_name) as 姓名 from employees;使用+运算符,+在mysql中仅是运算,若是数值型字符相加,可以将字符型数值进行转化,若不是数值型字符,则转换为0 select 'tom'+90; 结果为90

    模糊查询(属于条件查询)

    一.like like一般与通配符配合使用, 通配符:

    %:任意多个字符,包括0个字符。_:表示任意单个字符,就一个字符。 查询员工表中包含字符a的员工信息。 select * from employees where last_name like '%a%';查询员工表中第三个字符为a,第五个字符为b的员工信息。 select * from employees where last_name like '__a_b%';查询员工表中员工名第二个字符是_的员工名。 select last_name from employees where last_name like '_\_%'; 或者 select last_name from employees where last_name like '_$_%' escape '$'; $_表示是个转义,转的是_,至于$可以用任意字符替换,如: select last_name from employees where last_name like '_a_%' escape 'a';

    二.between and

    查询员工编号在100到200之间的员工信息。 select * from employees where employee_id >= 100 and employee_id <=200; 或者 select * from employees where employee_id between 100 and 200;

    注意:

    使用between and可以简练语句;使用between and包含两边的临界值;使用between and顺序不能颠倒,between 200 and 100是错误的。

    三. IN关键字

    查询id在001,003之间的员工姓名和工作id。 select last_name, job_id from employees where job_id in('001','003'); 或者 select last_name,job_id from employees where job_id ='001' or job_id='003';

    四. IS NULL

    查询没有奖金的的员工名和奖金率。 select last_name, commission_pct from employees where commission_pac is null; 错误写法:=和<>不能判断NULL值。 select last_name, commission_pct from employees where commission_pac = null;查询有奖金的的员工名和奖金率。 select last_name, commission_pct from employees where commission_pac is not null;

    五. 安全等于<=>

    查询没有奖金的的员工名和奖金率。 select last_name, commission_pct from employees where commission_pac <=> null;查询工资为1200的员工信息。 select * from employees where salary <=> 1200;

    六. IFNULL

    查询员工的姓名和年薪。 select last_name, salary*12*(1+IFNULL(commission_pac,0)) from employees;

    排序查询

    语法: select 查询列表 from 表 where 筛选条件 order by 排序列表 asc(升序,默认)或desc(降序);

    查询部门编号大于等于90的员工信息,并按入职时间先后进行排序。 select * from employees where department_id>=90 order by hiredate asc;按年薪的高低显示员工的信息和年薪。 select *, salary*12*(1+ifnull(commission_pct,0)) as 年薪 from employees order by salary * 12*(1+ifnull(commission_pct,0)) desc;按年薪的高低显示员工的信息和年薪(按别名排序)。 select *, salary*12*(1+ifnull(commission_pct,0)) as 年薪 from employees order by 年薪 desc;查询员工的姓名和工资并以员工姓名的长度进行排序(按函数排序)。 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; 结果是先按工资从低到高排序,若有工资相等的,则按员工编号从高到低显示。

    总结: 1.asc表示升序,desc表示降序,默认是升序; 2.order by后可以支持单个字段,多个字段组合,表达式,函数,别名; 3.order by一般放在语句的最后面,limit子句除外。

    最新回复(0)