语法:select (选择,过滤,查看)查询列表
Select 查询列表 from 表名;
类似于:system.out.println(打印东西)
特点:查询列表可以是:表中的字段/常量值,表达式,函数;
查询结果是一个虚拟的表格。
- 查询表中的单个字段 select last_name From employees;
- 查询表中的多个字段 select last_name,salary,email From employees;
- 查询表汇总的所有字段select * From employees;
查询细节问题:
- 打开相应的库;use myemployees; use test;
- 执行谁,选中它执行就可以了
- 查询常量值:select 100;没有字符和字符串概念:select ‘john’;
- 查询表达式:select 100%98;
- 查询函数:select version();
- 起别名:方便理解,写的少;查询的时候有重名情况, 使用别名可以区分开来;
方式一: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;
去重
Select distinct department_id from employees;
+号的作用:运算符
员工名和姓链接成一个字段,并显示为姓名
Select last_name+first_name AS 姓名 From employees;
在Java中+号作用:运算符(两个操作数都是数值型),连接符只要有一个操作数为字符串;
Select 100+90;两个操作数都为数值型,则做加法运算;
Select’123’+90,其中一方为字符型,试图将字符型数值转换成数值型,如果转换成功,则继续做加法运算。如果转换失败,则将字符型数值转换成0;
Select’john’+90;
Select null+10;只要其中一方为null,则结果肯定为null;
concat连接
Select concat(‘a’,’b’,’c’)AS 结果;abc
Select concat(last_name,first_name)AS 姓名 FROM employees;
测试题:
- 下面语句是否可以执行成功:
Select last_name ,job_id,salary AS sal FROM employees;
Select * from employees;(两句都可以执行成功)
- 显示表departments的结构,并查询其中的全部数据;
DESC departments;
Select * from ‘departments’;
- 显示表employees中全部的job_id(不能重复);
Select Distinct job_id from employees;
- 显示出表employees的全部列,各个列之间用逗号连接,列头显示成out_put;
select ifnull(//判断是否为空commission_pct,0)as 奖金率,commission_pct FROM employees;
Select concat(‘first_name’,’,’,’last_name’, ‘,’ ,’job_id’,’,’,ifnull(commission_pct,0) as out_put from employees;
条件查询
A、语法:select 查询列表 from 表名 where 筛选条件;
B、分类:第一类:按照条件表达式筛选
条件运算符:> < = <>
例如:查询工资大于12000的员工信息
Select * from employees where salary>112000;
查询部门编号不等于90号的员工名和部门编号
Select last_name,department_id from employees where department_id<>90;
第二类:按照逻辑表达式筛选
逻辑运算符: && || !
And or not
&&和and:如果两个条件都为true,结果为true,反之为false;
||或or:只要有一个条件为true,结果为true,反之为false;
!或not:如果连接条件本身为false,结果为true,反之为false;
例如;查询工资z再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;
第三类:模糊查询: like, between and ,in, is null
Like:一般和通配符搭配使用
通配符:%任意多个字符;_任意单个字符;
查询员工名中包含字符a的员工信息;
Select * from employees where last_name like ‘%a%’;
查询员工名中第三个字符为e,第五个字符为a的员工名和工资;
Select last_name,salary from employees where last_Name like ‘_n_l%’;
查询员工名中第二个字符为_的员工名
Select last_name from employees where last_name like ‘_\_%’;(\代表转译)
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:查询员工的公众编号是:IT_PROt.\AD_VP\AD_pres中的一个员工名和工种编号
Select last_name,job_id from employees where job_id=’IT_prot’ or job_id=’AD_vp’ or job_id =’ad_pres’;
用IN的是:判断某字段的值是否属于IN列表中的某一项;
特点:使用in提高语句简洁度;
In列表的值类型必须一致或兼容;
Select last_name,job_id from employees where job_id IN(’IT_prot’ ,AD_vp’ ,’ad_pres’);
- Is null
=或<>不能用于判断null值;
Is null或is not null 可以判断 null值;
查询没有奖金的员工名和奖金率;
Select last_name,commission_pct from employees where commision_pct IS NULL;
查询有奖金的员工名和奖金率;
Select last_name,commission_pct from employees where commision_pct IS NOT NULL;
补充:安全等于<=>:判断是否等于,可读性比较差,所以用的比较少;
Is null 仅仅判断null值;
<=>既可以判断null值,又可以判断普通数值,可读性比较低;
查询没有奖金的员工名和奖金率;
Select last_name,commission_pct from employees where commision_pct<=> NULL;
测试题:
- 查询没有奖金,且工资小于18000的salary,last_Name;
Select salary,last__name from employees where commision_pct is null and salary<18000
2、查询employees表中,job_id 不为’IT‘或工资12000的员工信息
Select * from employees where job _id<>’IT’ OR salary =12000;
3、查看部门departments表的结构
Desc department;
4、查询部门departments表中设计到了那些位置编号
Select distinct location_id from departments;
5、经典面试题:
试问select * from employees; 和 select * from employees where commission_pct like ‘%%’ and last_name ‘%%’;结果是否一样,并说明原因;
不一样,如果判断的字段有null值,结果一样,如果没有null值结果一样;