JavaMySQL数据库的使⽤:(四)MySQL最全查询数据⽅式-8000字匠⼼出品MySQL 查询数据
1.MySQL 的基本查询
1.MySQL 的列选择
SELECT*|投影列FROM表名
查询 departments 表中的所有数据
网页设计中span是什么意思
select*from departments;
2.MySQL 的⾏选择
SELECT*|投影列FROM表名WHERE选择条件电脑哪个键能结束进程
查询 departments 表中部门 ID 为 4 的部门名称与⼯作地点 ID。
select department_name,location_id from departments where department_id =4;
3.SELECT 语句中的算术表达式
加法运算:+
减法运算:-
乘法运算:*
除法运算,返回商 :/
求余运算,返回余数:%
计算 employees 表中的员⼯全年薪⽔加 100 以后的薪⽔是多少?
select employees_id,last_name,email,12*salary+100from employees;
4.MySQL 中定义空值
包含空值的算术表达式计算结果为空。
在 employees 中添加 commission_pct,计算年薪包含佣⾦。
alter table employees add column commission_pct float(5,2);
千锋前端培训怎么样select12*salary*commission_pct from employees;
5.MySQL 中的列别名
SELECT列名AS列别名FROM表名WHERE条件
查询 employees 表将雇员 laser_name 列名改为 name。
select last_name as name from employees;
6.MySQL 中的连字符
MySQL 中并不⽀持||作为连字符,需要使⽤ concat 函数。在参数数量上与 oracle 的 concat 函数有区别查询雇员表中的所有数据,将所有数据连接到⼀起,每列值中通过#分割。
select concat(employees_id,'#',last_name,'#',email,"#",salary,"#",commission_pct)
from employees;
7.MySQL 中去除重复
在 SELECT 语句中⽤ DISTINCT 关键字除去相同的⾏。
查询 employees 表,显⽰唯⼀的部门 ID。
select distinct dept_id from employees;
2.约束和排序数据
1.MySQL 中的⽐较条件
1.⽐较运算符
等于=
⼤于>
⼤于等于>=
⼩于<
⼩于等于<=
不等于!=或<>
查询 employees 表,员⼯薪⽔⼤于等于 3000 的员⼯的姓名与薪⽔。
select*from employees where salary >=3000;
2.模糊查询
like
%表⽰任意多个任意字符
_表⽰⼀个任意字符
查询 employees 中雇员名字第⼆个字母是 e 的雇员信息。
select*from employees where last_name like'_e%'
3.逻辑运算符
and
or
not
查询 employees 表中雇员薪⽔是 5000 的并且名字中含有 d 的雇员信息
select*from employees where salary =5000and last_name like'%e%'
4.范围查询
between … and
in 表⽰在⼀个⾮连续的范围内
查询 employees 表,薪⽔在 3000-8000 之间的雇员信息
select*from employees where salary between3000and8000
5.空值判断
判断空 is null
网站怎么注册的?判断⾮空 is not null
出 emloyees 表中那些没有佣⾦的雇员
select*from employees where commission_pct is null;
2.使⽤ ORDER BY 排序
⽤ ORDER BY ⼦句排序
ASC: 升序排序,默认
DESC: 降序排序
查询 employees 表中的所有雇员,薪⽔按升序排序。
select*from employees order by salary
3.MySQL 中常见的单⾏函数
1.⼤⼩写控制函数
LOWER(str) 转换⼤⼩写混合的字符串为⼩写字符串
UPPER(str) 转换⼤⼩写混合的字符串为⼤写字符串
2.字符处理
函数作⽤
CONCAT(str1,str2,…)将 str1、str2 等字符串连接起来
SUBSTR(str,pos,len)从 str 的第 pos 位(范围:1~str.length)开始,截取长度为 len 的字符串LENGTH(str)获取 str 的长度
INSTR(str,substr)获取 substr 在 str 中的位置
TRIM(str)从 str 中删除开头和结尾的空格(不会处理字符串中间含有的空格)
LTRIM(str)从 str 中删除左侧开头的空格
RTRIM(str)从 str 中删除右侧结尾的空格REPLACE(str,from_str,to_str)将 str 中的 from_str 替换为 to_str(会替换掉所有符合 from_str 的字符串)3.数字函数
函数作⽤
ROUND(arg1,arg2)四舍五⼊指定⼩数的值。
ROUND(arg1)四舍五⼊保留整数
TRUNC(arg1,arg2)截断指定⼩数的值,不做四舍五⼊处理
MOD(arg1,arg2)取余
4.⽇期函数
函数作⽤
SYSDATE() 或者 NOW()返回当前系统时间,格式为 YYYY-MM-DD hh-mm-ss
CURDATE()返回系统当前⽇期,不返回时间
函数作⽤
CURTIME()返回当前系统中的时间,不返回⽇期
DAYOFMONTH(date)计算⽇期 d 是本⽉的第⼏天
DAYOFWEEK(date)⽇期 d 今天是星期⼏,1 星期⽇,2 星期⼀,以此类推
DAYOFYEAR(date)返回指定年份的天数
DAYNAME(date)返回 date ⽇期是星期⼏
LAST_DAY(date)返回 date ⽇期当⽉的最后⼀天
5.转换函数
函数作⽤
write的中文DATE_FORMAT(date,format)将⽇期转换成字符串(类似 oracle 中的 to_char())
STR_TO_DATE(str,format)将字符串转换成⽇期(类似 oracle 中的 to_date())
向 employees 表中添加⼀条数据,名字:King ,email:king@sxt,部门 ID:1,薪⽔:9000,⼊职时间:2018 年 5 ⽉ 1 ⽇,佣⾦:0.6
insert into employees values(
default,'King',
'king@sxt',
1,9000,0.6,
STR_TO_DATE('2018 年 5 ⽉ 1 ⽇','%Y 年%m ⽉%d ⽇'))
查询 employees 表中雇员名字为 King 的雇员的⼊职⽇期,要求显⽰格式为 yyyy 年 MM ⽉ dd ⽇
select DATE_FORMAT(hire_date,'%Y 年%m ⽉%d ⽇')from employees where last_name ='King'
6.通⽤函数
函数作⽤
IFNULL(expr1,expr2)判断 expr1 是否为 null,如果为 null,则⽤ expr2 来代替 null (类似 oracle 的 NVL()函数)NULLIF(expr1,expr2)判断 expr1 和 expr2 是否相等,如果相等则返回 null,如果不 相等则返回 expr1
IF(expr1,expr2,expr3)判断 expr1 是否为真(是否不为 null),如果为真,则使⽤ expr2 替代 expr1;如果为假,则使⽤ expr3 替代 expr1(类似
mysql语句多表查询
oracle 的 NVL2()函数)
COALESCE(value,…)判断 value 的值是否为 null,如果不为 null,则返回 value;如果为 null,则判断下⼀个 value 是否为 null……直⾄出现不为
null 的 value 并返回或者返回最后⼀个为 null 的 value
函数作⽤
4.多表连接查询
1.等值连接
查询雇员 King 所在的部门名称
select d.department_name
from employees e,departments d
where e.dept_id = d.department_id and e.last_name ='King'
2.⾮等值连接
查询所有雇员的薪⽔级别。
select e.last_name,s.level
from employees e ,sal_level s
where e.salary between s.lowest_sal and highest_sal;
3.⾃连接
查询每个雇员的经理的名字以及雇员的名字。
select emp.last_name,man.last_name
from employees emp ,employees man
where emp.manager_id = ployees_id
5.外连接(OUTER JOIN)
1.左外连接(LEFT OUTER JOIN)
查询所有雇员的名字以及他们的部门名称,包含那些没有部门的雇员。
select e.last_name,d.department_name
from employees e
LEFT OUTER JOIN departments d on e.dept_id = d.department_id
2.右外连接(RIGHT OUTER JOIN)
查询所有雇员的名字以及他们的部门名称,包含那些没有雇员的部门。
select e.last_name,d.department_name
from employees e
right OUTER join departments d on e.dept_id = d.department_id;
3.全外链接
注意:MySQL 中不⽀持 FULL OUTER JOIN 连接
可以使⽤ union 实现全完连接。
1.UNION
可以将两个查询结果集合并,返回的⾏都是唯⼀的,如同对整个结果集合使⽤了 DISTINCT。
2.UNION ALL

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。