Oracle关联查询关于left/right join的那点事
/*题外话
--更改foreign key约束定义引用行(delete cascade/delete set null/delete no action),默认delete on action
--引用行(当主表条记录被删除时确定何处理字表外部码字段):
--delete cascade : 删除子表所有相关记录
--delete set null : 所有相关记录外部码字段值设置NULL
--delete no action: 做任何操作
--left 以左表为主,左表中的数据都查询出来
--约束唯一 unique
--多对多
*/
drop table stud;
drop table course;
select * from USER_TABLES;
--创建学生表
create table stud(
id int primary key,
name varchar(30)
);
--课程表
create table course(
id int primary key,
name varchar(30),
hours int
);
--
create table sc (
sid int ,
cid int,
constraint sc_pk primary key(sid,cid),
constraint sc_fk1 FOREIGN key(sid) references stud(id),
constraint sc_fk2 FOREIGN key(cid) references course(id)
);
select * from sc;
--先写入几个学生
insert into stud values(1,'Jack');
insert into stud values(2,'张三');
insert into stud values(3,'李四');
insert into stud values(4,'Rose');
--再写入几个课程
insert into course values(101,'Java',120);
insert into course values(102,'C#',60);
insert into course values(103,'Oracle',75);
insert into course values(104,'.NET',60);
commit;
--开始选课
insert into sc values (1,101);
insert into sc values (1,102);
insert into sc values (2,101);
insert into sc values (3,104);
commit;
-----------------------------------------------
-------------------开始查询---------------------
-----------------------------------------------
--查学生选了什么课
select s.name as 学生,c.name as 成绩
from stud s,course c,sc
where s.id=sc.SID and c.id=sc.CID;
-----inner join
select s.name ,c.name from stud s
inner join sc on s.id=sc.sid
inner join course c on c.id=sc.cid;
--查询没有选课的
select s.name,c.name from stud s
LEFT join sc on s.id=sc.SID
LEFT join COURSE c on c.ID=sc.cid
where c.name is null;
select s.name from course c
right join sc on c.id=sc.cid
right join stud s on s.id=sc.sid
where c.name is null;
--查询那些课没人选
select s.name,c.name as cname
from stud s
right join sc on s.id=sc.sid
right join course c on sc.cid=c.id
where s.name is null;
select s.name,c.name as cname
from course c
left join sc on c.id=sc.cid
left join stud s on sc.sid=s.id
where s.name is null;
Oracle关联查询关于left/right join的那点事的更多相关文章
- Oracle关联查询-数据类型不一致问题 ORA-01722: 无效数字
一.存在表A和表B,都包含字段user_no,但数据类型不一致,如下: create table A ( user_id varchar2(20), user_no number(12,0), xxx ...
- Yii2中多表关联查询(with、join、joinwith)
表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name custome ...
- 【database】oracle关联查询主表对应的特定一行从表结果集
主表: 从表: 结果集: 查询从表中年龄最大的一行数据,如果存在年龄相等的则为了保证唯一取id(主键)最大的一行. 一.利用sql子查询嵌套 -- -------------------------- ...
- 关联查询总结,left join 和 inner join 区别和优化
left join 是做左外关联,主表内容都会显示:符合关联条件的附表内容才会显示出来. inner join 是内关联,没有主表附表的概念:两个表中,同时符合关联条件的数据才会显示出来. left ...
- Oracle 关联查询
select count(1),a.policy_id from gp_pol_prod a where a.product_id=8401 group by a.policy_id having c ...
- JDBC MySQL 多表关联查询查询
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...
- SQL-Server多表关联查询并分页
一.多表关联查询 1,left join RelaTimeLog表 和 ValidFlight表关联查询 order by t.FlightId desc 2,与group by连用 group by ...
- CI 多表关联查询
方法一:$this->db->query("sql 语句"); 直接写sql语句 方法二: #多表关联查询 $data=$this->db->fr ...
- EF Core 的关联查询
0 前言 本文会列举出 EF Core 关联查询的方法: 在第一.二.三节中,介绍的是 EF Core 的基本能力,在实体中配置好关系,即可使用,且其使用方式,与编程思维吻合,是本文推荐的方式. 第四 ...
随机推荐
- Mongodb基本操作之.net
1.下载官方for C#驱动 2.导入2个dll文件 3.连接字符串 <add key="MongoConn" value="mongodb://127.0.0.1 ...
- js中关于一个数组中最大、最小值以及它们的下标的输出的一种解决办法
今天在学习js中的数组时,遇到的输出一个数组中最大.最小值以及它们的下表,以下是自己的解决方法! <script type="text/javascript"> var ...
- 武汉科技大学ACM :1010: 华科版C语言程序设计教程(第二版)例题7.8
Problem Description 输入一个用年月日表示的日期,求该日期是该年的第几天.输入某年的第几天,输出这一天是该年的几月几号,茂茂解不出,需要你的帮助. Input 开始有个整数k,表示询 ...
- 逆向并查集 hrbust 1913
#include<iostream> //由于拆除并查集的方法太难或者没有#include<cstdio> //可以先将所有没有拆的桥连接 再逆向操作 断开变成连接 反向输出# ...
- (原)python中matplotlib的颜色及线条控制
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6117528.html 参考网址: http://stackoverflow.com/questions ...
- centos 安装,配置memcached
先查看是否已经安装了memcached输入memcached -h会输出memcached版本,或print phpinfo查看: memcached需要libevent支持,没有libevent,就 ...
- String 和 string 的区别
string是c#中的类,String是.net Framework的类(在c# IDE中不会显示蓝色)c# string映射为.net Framework的String如果用string,编译器会把 ...
- 【Beta】Scrum10
Info 最后一次Scrum会议(补发) 时间:2017.01.03 21:35 时长:20min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:-- Task Report Nam ...
- STL string常用操作指令
s.insert(pos,args); 在pos之前插入args指定的字符.pos可以是一个下标或一个迭代器.接受下标的版本返回一个指向s的引用;接受迭代器的版本返回指向第一个插入字符的迭代器. s. ...
- mysql出现错误“ Every derived table must have its own alias”
Every derived table must have its own alias 这句话的意思是说每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时 ...