SQL基础知识(下)

一.多表查询

如果直接查询

select name,course_name from student,course;

会产生笛卡尔集
笛卡尔集产生条件:
1.省略连接条件
2.连接条件无效
3.所有表中的所有行互相连接
为了避免笛卡尔集,可以在where中加入有效的连接条件

使用on子句创建链接
内连接:[inner] join on
外连接:
· 左外连接:left[outer]join on
· 右外连接:right[outer]join on
连接单个表:

select gril.id,grilname,boyname from gril inner join boy on gril.boyfriend_id=boy.id;

连接多个表:

select employee_id,city,department_name
from employees
join departments
on department.departments_id=employees.department_id
join locations
on departments.location_id = locations.location_id;

子查询

例如:

select last_name
from employees
where salary > (select salary from employees where last_name = 'Able');

多行子查询

.返回多行
.使用多行比较操作符
in/not in 等于列表中的任意一个
any/some 和子查询返回的某一值比较
all 和子查询返回的所有值比较
例如:使用any操作符
返回其他部门中比job.idIT_PROG部门任一工资低的员工的信息

select employee_id,last_name,job_id,salary
from employees
where salary < any(select salary
    from employees
    where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';

使用all操作符
返回其他部门中比job.idIT_PROG部门所有工资低的员工的信息

select employee_id,last_name,job_id,salary
from employees
where salary < all(select salary
    from employees
    where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值