日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQL教程——连接查询之SQL99标准

發(fā)布時(shí)間:2023/12/14 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL教程——连接查询之SQL99标准 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本教程中所使用的數(shù)據(jù)庫的建表語句都在“SQL教程——索引”這篇文章中,點(diǎn)擊鏈接直達(dá):索引&建表語句

摘要:本文主要介紹SQL99標(biāo)準(zhǔn)的連接查詢

?

SQL99標(biāo)準(zhǔn)

語法:

? ? select?查詢列表

? ? from?表1?別名 【鏈接類型】

? ? join? ?表2 別名

? ? on?鏈接條件

? ? 【where?篩選條件】

? ? 【group?by?分組】

? ? 【having?篩選條件】

? ? 【order?by?排序】

?

分類:

  • 內(nèi)連接(※)inner

  • 外連接

  • 左外(※):left【outer】

  • 右外(※):right【outer】

  • 全外:full【outer】

  • 交叉連接:cross

  • ?

    1、內(nèi)連接

    語法:

    ? ? select?查詢列表

    ? ? from?表1?別名

    ? ? inner?join?表2?別名

    ? ? on?鏈接條件

    ?

    分類:

  • 等值連接

  • 非等值連接

  • 自連接

  • 特點(diǎn):

    • 添加排序、分組、篩選。

    • inner可以省略。

    • 篩選條件放在where后面,連接誒條件放在on后面,提高分離性,便于閱讀。

    • inner?join鏈接和sql92語法中的的等值連接效果是一樣的。

    ?

    ?

    #1、等值連接

    #案例1:查詢員工名、部門名select last_name, department_namefrom employees einner join departments don e.department_id = d.department_id;#2、查詢名字中包含e的員工名和工種名(添加篩選)select last_name, job_titlefrom employees einner join jobs jon j.job_idwhere e.last_name like '%e%';#3、查詢部門個(gè)數(shù)>3的城市名和部門個(gè)數(shù),(分組+篩選)select city, count(*)from departments dinner join locations lon d.location_id= l.location_idgroup by cityhaving count(*) > 3;#4、查詢哪個(gè)部門的員工個(gè)數(shù)>3的部門名和員工個(gè)數(shù),并按照個(gè)數(shù)降序(添加排序)select department_name, count(*)from employees einner join departments don e.department_id = d.department_idgroup by department_namehaving count(*) > 3order by count(*) desc;#5、查詢員工名、部門名、工種名,并按部門名降序select last_name, department_name, job_titlefrom employees einner join departments d on e.department_id = d.department_idinner join jobs j on e.job_id = j.job_idorder by department_name desc;

    #2、非等值連接

    #查詢員工的工資級(jí)別select last_name, grade_levelfrom employees ejoin job_grades jon e.salary between j.lowest_sal and j.highest_sal;#查詢員工工資級(jí)別個(gè)數(shù)>2的級(jí)別個(gè)數(shù),并按工資級(jí)別排序select count(*), grade_levelfrom employees ejoin job_grades jon e.salary between j.lowest_sal and j.highest_salgroup by grade_levelhaving count(*) > 2order by grade_level desc;

    #3、自連接

    #查詢員工的名字、上級(jí)的名字select e.last_name?員工, m.last_name?上級(jí)員工from employees einner join employees mon e.manager_id = m.employee_id;

    ?

    2、外連接

    ?

    應(yīng)用場(chǎng)景:用于查詢一個(gè)表中有,另一個(gè)表中沒有的情況

    特點(diǎn):

  • 外連接查詢結(jié)果為主表中的所有記錄

    如果從表中有和它匹配的記錄,則顯示匹配的

    如果從表中沒有和它匹配的記錄,則顯示null值

    左右外鏈接查詢結(jié)果 =?內(nèi)連接結(jié)果 +?主表中有而從表沒有的記錄

    全外連接不分主表從表,它的查詢結(jié)果包括兩表交集+A表有B表沒有的部分+B表有A表沒有的部分

  • 左外連接,left?join左邊是主表

    右外鏈接,right?join右邊的是主表

  • 左外和右外交換兩個(gè)表的順序,可以實(shí)現(xiàn)同樣的效果


  • ?

    #引入:查詢男朋友不在男神表的女神名#于是你可能會(huì)想到用嵌套查詢:select namefrom beauty gwhere boyfriend_id not in(select id from boys);但如果不讓用嵌套呢?兩種方法:左外:select b.name, bo.*from beauty bleft outer join boys boon b.boyfriend_id = bo.idwhere bo.id is null;右外:select b.name, bo.*from boys boright outer join?beauty bon b.boyfriend_id = bo.idwhere bo.id is null;#案例1:查詢哪個(gè)部門沒有員工#右外select d.*, e.employee_idfrom employees eright outer join departments don e.department_id = d.department_idwhere e.employee_id is null;#左外select d.*, e.employee_idfrom?departments dleft outer join employees eon e.department_id = d.department_idwhere e.employee_id is null;#全外(mysql不支持全外連接)select b.*, bo.*from beauty bfull outer join boys boon b.boyfriend_id = bo.id;

    3、交叉連接(笛卡爾乘)

    select b.*, bo.*from beauty bcross join boys bo;

    ?

    ?

    小結(jié):

    SQL92 VS SQL99

    • 功能:SQL99支持的較多

    • 可讀性:SQL99實(shí)現(xiàn)連接條件和篩選條件的分離,可讀性較高

    #Test#案例1:查詢編號(hào)>3的女神的男朋友信息,如果有則列出詳細(xì),如果沒有,用null填充select b.name, bo.*from beauty bleft outer join boys boon b.boyfriend_id = bo.idwhere b.id > 3;#案例2:查詢哪個(gè)城市沒有部門select city, department_namefrom locations lleft outer join departments don l.location_id = d.location_idwhere department_id is null;#案例3:查詢部門名為SAL或IT的員工信息select d.department_name, e.*from employees ejoin departments don e.department_id = d.department_idwhere d.department_name in ('SAL', 'IT');

    ?

    總結(jié)

    以上是生活随笔為你收集整理的SQL教程——连接查询之SQL99标准的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。