08_MySQL DQL_SQL99標準中的多表查詢(內連接)

# sql99語法
/*
語法:
select 查詢列表
from 表1 別名 【連接類型】
join 表2 別名
on 連接條件
【where 篩選條件】
【group by 分組】
【having 分組后篩選】
【order by 排序列表】

分類
內連接(重點): inner
外連接
左外(重點): left 【outer】
右外(類似左外) right 【outer】
全外: full 【outer】
交叉連接:cross


*/

# 一、內連接(就是把兩個表的字段全部連接,表間沒有順序)
/*
select 查詢列表
from 表1 別名
inner join 表2 別名 on 連接條件
inner join 表3 別名 on 連接條件
【where 篩選條件】
【group by 分組】
【having 分組后篩選】
【order by 排序列表】

分類:等值,非等值,自連接
*/


#1 等值連接
#案例1:查詢員工名,部門名 【等值內連】
SELECT last_name,department_name
FROM employees AS e
INNER JOIN departments AS d
ON e.`department_id` = d.`department_id`;

#案例2:查詢名字中包含e的員工名和工種名【等值內連 + 篩選】
SELECT last_name, job_title
FROM employees AS e
INNER JOIN jobs AS j
ON e.`job_id` = j.`job_id`
WHERE last_name LIKE '%e%';

#案例3:查詢部門個數>3的城市名和部門個數【等值內連+分組+分組后篩選】
SELECT city,COUNT(*) AS "部門個數"
FROM departments AS d
INNER JOIN locations AS l
ON d.`location_id` = l.`location_id`
GROUP BY l.`city`
HAVING 部門個數>3;

#案例4:查詢部門員工數>3的部門名稱和員工個數,并降序排序
#【等值連接+分組+分組后篩選+ 排序】
SELECT department_name, COUNT(*) AS "員工個數"
FROM departments AS d
INNER JOIN employees AS e
ON d.`department_id` = e.`department_id`
GROUP BY e.`department_id`
HAVING 員工個數 > 3
ORDER BY 員工個數 DESC;

#案例5:查詢員工名,部門名,工種名,并按部門名排序
#【多表等值連接+排序】
SELECT last_name, department_name, job_title
FROM employees AS e
INNER JOIN departments d ON e.`department_id` = d.`department_id`
INNER JOIN jobs AS j ON e.`job_id` = j.`job_id`
ORDER BY department_name DESC;


#2 非等值連接

#案例1:查詢員工的工資級別
SELECT salary,grade_level
FROM employees AS e
INNER JOIN job_grades AS j
ON e.salary BETWEEN j.`lowest_sal` AND j.`highest_sal`;


#案例2:查詢每個工資級別下員工個數》2的工資級別,并降序排列
SELECT COUNT(*), grade_level
FROM employees AS e
INNER JOIN job_grades AS j
ON e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`
GROUP BY grade_level
HAVING COUNT(*) > 20
ORDER BY COUNT(*) DESC;


#3 內連接中的自連接

#案例1:查詢員工的名字,及其上級的名字
SELECT e.last_name, m.last_name
FROM employees AS e
INNER JOIN employees AS m
ON e.`manager_id` = m.`employee_id`
ORDER BY e.`last_name` ASC;

#案例2:查詢名字中包含字符k的員工名,及其上級的名字
SELECT e.last_name, m.last_name
FROM employees AS e
INNER JOIN employees AS m
ON e.`manager_id` = m.`employee_id`
WHERE e.`last_name` LIKE '%k%'
ORDER BY e.`last_name` ASC;


轉載于:https://www.cnblogs.com/shay-zhangjin/p/7906611.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390695.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390695.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390695.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java中抽象類繼承抽象類_Java中的抽象類用示例解釋

java中抽象類繼承抽象類Abstract classes are classes declared with abstract. They can be subclassed or extended, but cannot be instantiated. You can think of them as a class version of interfaces, or as an interface with actual code attached to the methods.抽…

新建VUX項目

使用Vue-cli安裝Vux2 特別注意配置vux-loader。來自為知筆記(Wiz)

衡量試卷難度信度_我們可以通過數字來衡量語言難度嗎?

衡量試卷難度信度Without a doubt, the world is “growing smaller” in terms of our access to people and content from other countries and cultures. Even the COVID-19 pandemic, which has curtailed international travel, has led to increasing virtual interactio…

Linux 題目總結

守護進程的工作就是打開一個端口,并且等待(Listen)進入連接。 如果客戶端發起一個連接請求,守護進程就創建(Fork)一個子進程響應這個連接,而主進程繼續監聽其他的服務請求。 xinetd能夠同時監聽…

《精通Spring4.X企業應用開發實戰》讀后感第二章

一、配置Maven\tomcat https://www.cnblogs.com/Miracle-Maker/articles/6476687.html https://www.cnblogs.com/Knowledge-has-no-limit/p/7240585.html 二、創建數據庫表 DROP DATABASE IF EXISTS sampledb; CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8; USE sampl…

換了電腦如何使用hexo繼續寫博客

前言 我們知道,使用 Githubhexo 搭建一個個人博客確實需要花不少時間的,我們搭好博客后使用的挺好,但是如果我們有一天電腦突然壞了,或者換了系統,那么我們怎么使用 hexo 再發布文章到個人博客呢? 如果我們…

leetcode 525. 連續數組

給定一個二進制數組 nums , 找到含有相同數量的 0 和 1 的最長連續子數組,并返回該子數組的長度。 示例 1: 輸入: nums [0,1] 輸出: 2 說明: [0, 1] 是具有相同數量 0 和 1 的最長連續子數組。 示例 2: 輸入: nums [0,1,0] 輸出: 2 說明: [0, 1] (或 [1, 0]) 是…

實踐作業2:黑盒測試實踐(小組作業)每日任務記錄1

會議時間:2017年11月24日20:00 – 20:30 會議地點:在線討論 主 持 人:王晨懿 參會人員:王晨懿、余晨晨、鄭錦波、楊瀟、侯歡、汪元 記 錄 人:楊瀟 會議議題:軟件測試課程作業-黑盒測試實踐的啟動計劃 會議內…

視圖可視化 后臺_如何在單視圖中可視化復雜的多層主題

視圖可視化 后臺Sometimes a dataset can tell many stories. Trying to show them all in a single visualization is great, but can be too much of a good thing. How do you avoid information overload without oversimplification?有時數據集可以講述許多故事。 試圖在…

iam身份驗證以及訪問控制_如何將受限訪問IAM用戶添加到EKS群集

iam身份驗證以及訪問控制介紹 (Introduction) Elastic Kubernetes Service (EKS) is the fully managed Kubernetes service from AWS. It is deeply integrated with many AWS services, such as AWS Identity and Access Management (IAM) (for authentication to the cluste…

一步一步構建自己的管理系統①

2019獨角獸企業重金招聘Python工程師標準>>> 系統肯定要先選一個基礎框架。 還算比較熟悉Spring. 就選Spring boot postgres mybatis. 前端用Angular. 開始搭開發環境,開在window上整的。 到時候再放到服務器上。 自己也去整了個小服務器,…

面向對象面向過程

1、面向語句: 直接寫原生的sql語句,但是這樣代碼不容易維護。改一個方法會導致整個項目都要改動, 2、面向過程 定義一些函數,用的時候就調用不用就不調用。但是這也有解決不了的問題,如果要維護需要改動代碼&#xff0…

python邊玩邊學_邊聽邊學數據科學

python邊玩邊學Podcasts are a fun way to learn new stuff about the topics you like. Podcast hosts have to find a way to explain complex ideas in simple terms because no one would understand them otherwise 🙂 In this article I present a few episod…

react css多個變量_如何使用CSS變量和React上下文創建主題引擎

react css多個變量CSS variables are really cool. You can use them for a lot of things, like applying themes in your application with ease. CSS變量真的很棒。 您可以將它們用于很多事情,例如輕松地在應用程序中應用主題。 In this tutorial Ill show you …

vue 自定義 移動端篩選條件

1.創建組件 components/FilterBar/FilterBar.vue <template><div class"filterbar" :style"{top: top px}"><div class"container"><div class"row"><divclass"col":class"{selected: ind…

PSP

姓名&#xff1a;袁亞琴 日期&#xff1a;11月27日 教師&#xff1a;王建民 課程&#xff1a;PSP 項目計劃日志&#xff1a; PSP Planning . Estimate Development . Analysis . Design Spec . Design Review . …

如何在Windows中打開和使用命令提示符

入門 (Getting started) Windows, MacOS and Linux have command line interfaces. Windows’ default command line is the command prompt. The command prompt allows users to use their computer without pointing and clicking with a mouse. Windows&#xff0c;MacOS和…

ACM-ICPC北京賽區2017網絡同步賽H

http://hihocoder.com/contest/icpcbeijing2017/problem/8 預處理暴力枚舉修改的點 #include <bits/stdc.h> using namespace std; const int maxn 159; const int inf 0x3f3f3f3f; int a[maxn][maxn]; int colsum[maxn][maxn]; int rowsum[maxn][maxn]; int dp[maxn];…

PPPOE撥號上網流程及密碼竊取具體實現

樓主學生黨一枚&#xff0c;最近研究netkeeper有些許心得。 關于netkeeper是調用windows的rasdial來進行上網的東西&#xff0c;網上已經有一大堆&#xff0c;我就不贅述了。 本文主要講解rasdial的部分核心過程&#xff0c;以及我們可以利用它來干些什么。 netkeeper中rasdial…

leetcode 160. 相交鏈表(雙指針)

給你兩個單鏈表的頭節點 headA 和 headB &#xff0c;請你找出并返回兩個單鏈表相交的起始節點。如果兩個鏈表沒有交點&#xff0c;返回 null 。 圖示兩個鏈表在節點 c1 開始相交&#xff1a; 題目數據 保證 整個鏈式結構中不存在環。 注意&#xff0c;函數返回結果后&#…