多表關聯查詢之內關聯,左關聯


-- 同學持有的手機記錄表

create table student_phone (
id int primary key,
stu_name varchar2(20),
phone varchar2(200)
)

insert into student_phone values (1,'小紅','華為 榮耀v9');
insert into student_phone values (2,'小軍','榮耀v9(華為)');
insert into student_phone values (3,'小紅','小米note2');
insert into student_phone values (4,'小平','小米 note2');
insert into student_phone values (5,'小平','小米 note2');
insert into student_phone values (6,'小雲','小米 Note2');
insert into student_phone values (7,'小雲','小米 NOTE2');
insert into student_phone values (8,'小雲','小米 NOTE 2');

 

commit

select phone,count(1) t from student_phone
group by phone order by t desc
---------------------------
drop table student_phone

create table student_phone (
id int primary key,
stu_name varchar2(20),
phone int
)

create table phone(
id int primary key,
phone_name varchar2(200)
)
insert into phone values(1,'榮耀v9(華為)');
insert into phone values(2,'小米 note2');
commit

select * from phone
select * from student_phone

insert into student_phone values (1,'小紅',1);
insert into student_phone values (2,'小軍',1);
insert into student_phone values (3,'小紅',2);
insert into student_phone values (4,'小平',2);
insert into student_phone values (5,'小平',2);
insert into student_phone values (6,'小雲',2);
insert into student_phone values (7,'小雲',2);
insert into student_phone values (8,'小雲',2);
commit

select phone,count(1) t from student_phone
group by phone order by t desc

可以通過phone=id
select * from student_phone,phone
where student_phone.phone=phone.id

------------------------------
select a.stu_name,b.phone_name from student_phone a,phone b
where a.phone=b.id

-------------------------------------
select phone_name,count(1) 總人數 from
(
select a.stu_name,b.phone_name from student_phone a,phone b
where a.phone=b.id
) ttttt
group by phone_name order by 總人數 desc

----------------------------

-- 多表關聯
-- 內關聯跟左關聯只是改一下關鍵字inner—>left
-- 內關聯
select * from student_phone,phone
where student_phone.phone=phone.id
-- 1.內關聯 (丟失不滿足的記錄條數)
select * from student_phone a
inner join
phone b on a.phone=b.id

 


--
insert into student_phone values (9,'小成',3);
commit
select * from phone
select * from student_phone


select a.stu_name,b.phone_name from student_phone a
inner join
phone b on a.phone=b.id
--測試
insert into phone values(3,'iphone8(美國總統專用)');
commit
delete from phone where id=3

丟失了小成

-- 2.左關聯

select a.stu_name,b.phone_name from student_phone a
left join
phone b on a.phone=b.id

-- 改進一下
select a.stu_name,nvl(b.phone_name,'未知') from student_phone a
left join
phone b on a.phone=b.id

 

--回歸原始 (oracle專用+:即把丟失的+回來)
select a.stu_name,b.phone_name from student_phone a,phone b
where a.phone=b.id(+)

--求什么手機最多人使用(排序)
--分組[后]最大值
select * from(
select phone,count(1) counts from student_phone group by phone
order by counts desc
)
where rownum=1


select * from(
select phone,count(1) counts from student_phone group by phone
order by counts asc
)
where rownum=1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM