主頁 > 知識(shí)庫 > MySql中子查詢內(nèi)查詢示例詳解

MySql中子查詢內(nèi)查詢示例詳解

熱門標(biāo)簽:地圖標(biāo)注客戶付款 申請(qǐng)400電話電話價(jià)格 許昌外呼增值業(yè)務(wù)線路 廣東400企業(yè)電話申請(qǐng)流程 石家莊400電話辦理公司 新鄉(xiāng)智能外呼系統(tǒng)好處 宜賓全自動(dòng)外呼系統(tǒng)廠家 咸陽防封電銷卡 臨沂做地圖標(biāo)注

  西北望鄉(xiāng)何處是,東南見月幾回圓。

月亮又慢悠悠的掛上了天空,趁著睡前夢(mèng)囈,我就帶領(lǐng)各位可愛的讀者們探索MySql最后的子查詢部分。

說明:有些查詢結(jié)果出來結(jié)果截圖與題目要求不一樣會(huì)出現(xiàn)多余的字段是為了方便展示結(jié)果的可讀性。實(shí)際操作的讀者可以刪除SELECT后面多余的字段得到正確的結(jié)果。

#WHERE或HAVING后面
#1.標(biāo)量子查詢(單行子查詢)
#2.列子查詢(多行子查詢)
#3.行子查詢(多列多行)
#特點(diǎn):
# ①子查詢放在小括號(hào)內(nèi)
# ②子查詢一般放在條件的右側(cè)
# ③標(biāo)量子查詢:一般搭配著單行操作符使用
#  單行操作符: >  >= = > !-
#  列子查詢,一般搭配著多行操作符使用
#  IN,ANY/SOME(任意),ALL
# ④子查詢的執(zhí)行優(yōu)先與主查詢執(zhí)行,主查詢的條件用到了子查詢的結(jié)果。
#1.標(biāo)量子查詢
#案例1:誰的工資比Abel高?
#①查詢Abel的工資
SELECT salary
FROM employees
WHERE last_name = 'Abel';

#②查詢員工的信息,滿足Salary>①結(jié)果
SELECT *
FROM employees
WHERE salary>(SELECT salary FROM employees WHERE last_name='Abel');

#案例2.返回job_id與141號(hào)員工相同,salary比143號(hào)員工多的員工姓名,job_id,工資。
#①查141員工的job_id
SELECT job_id
FROM employees
WHERE employee_id='141';

#②查143員工的salary
SELECT salary
FROM employees
WHERE employee_id='143';

#③最后合并結(jié)果
SELECT CONCAT(last_name,first_name) AS 姓名,
job_id AS 工種編號(hào),
salary AS 工資
FROM employees
WHERE job_id=(
 SELECT job_id
 FROM employees
 WHERE employee_id='141'
)
AND salary>(
 SELECT salary
 FROM employees
 WHERE employee_id='143'
);

#案例3.返回公司工資最少的員工的last_name,job_id和salary。
SELECT MIN(salary)
FROM employees;

SELECT
last_name AS 姓,
salary AS 工資,
job_id AS 工種編號(hào)
FROM employees
WHERE salary=(
 SELECT MIN(salary)
 FROM employees
     );

#案例4.查詢最低工資大于50號(hào)部門最低工資的部門id和其最低工資。
#①查50部門的最低工資
SELECT MIN(salary)
FROM employees
WHERE department_id=50;

#分組后,篩選條件①.【不用排除沒有部門的所以不篩選部門編號(hào)】
SELECT department_id AS 部門編號(hào),
MIN(salary) AS 月薪
FROM employees
#WHERE department_id
GROUP BY department_id
HAVING 月薪>(
 SELECT MIN(salary)
 FROM employees
    );

#2.列子查詢(多行子查詢)
#返回多行
#使用多行比較操作符

#案例1.返回location_id是1400或1700的部門中的所有員工姓名。
#①查詢location_id是1400或1700的部門編號(hào)
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700);

#②查詢員工姓名,要求部門號(hào)是①列表的某一個(gè)
SELECT CONCAT(last_name,first_name) AS 姓名
FROM employees
WHERE department_id IN (
 SELECT DISTINCT department_id
 FROM departments
 WHERE location_id IN(1400,1700)
 );

用ANY替代IN與上面同樣的結(jié)果
SELECT CONCAT(last_name,first_name) AS 姓名
FROM employees
WHERE department_id = ANY(
 SELECT DISTINCT department_id
 FROM departments
 WHERE location_id IN(1400,1700)
 );
#案例.返回location_id不是1400或1700的部門中的所有員工姓名。
SELECT CONCAT(last_name,first_name) AS 姓名
FROM employees
WHERE department_id NOT IN(
 SELECT DISTINCT department_id
 FROM departments
 WHERE location_id IN(1400,1700)
);
==============================
SELECT CONCAT(last_name,first_name) AS 姓名
FROM employees
WHERE department_id > ALL(
 SELECT DISTINCT department_id
 FROM departments
 WHERE location_id IN(1400,1700)
);

#案例2.返回其他工種中比job_id為IT_PROG部門任意一工資低的員工工號(hào),
# 姓名,job_id以及salary
#①把IT_PROG部門中的工資查出來
SELECT DISTINCT salary
FROM employees
WHERE job_id='IT_PROG';

#②把不是IT_PROG部門信息查出來
SELECT *
FROM employees
WHERE job_id != 'IT_PROG';

#③合并①與②在員工表中查出來
SELECT employee_id AS 員工編號(hào),
CONCAT(last_name,first_name) AS 姓名,
job_id AS 工種編號(hào),
salary AS 工資
FROM employees
WHERE job_id != 'IT_PROG'
AND salaryANY(
 SELECT salary
 FROM employees
 WHERE job_id='IT_PROG'
  );

用MAX代替ANY與上面同樣的效果
SELECT employee_id AS 員工編號(hào),
CONCAT(last_name,first_name) AS 姓名,
job_id AS 工種編號(hào),
salary AS 工資
FROM employees
WHERE job_id > 'IT_PROG'
AND salary(
 SELECT MAX(salary)
 FROM employees
 WHERE job_id='IT_PROG'
  );
#案例3.返回其他部門中比job_id為‘IT_PROG'部門所有工資都低的員工
#的員工號(hào),姓名,job_id以及salary。
#①先把IT_PROG部門的工資查出來。
SELECT DISTINCT salary
FROM employees
WHERE job_id='IT_PROG';

SELECT employee_id AS 員工號(hào),
CONCAT(last_name,first_name) AS 姓名,
job_id AS 工種編號(hào),
salary AS 工資
FROM employees
WHERE salaryALL(
 SELECT DISTINCT salary
 FROM employees
 WHERE job_id='IT_PROG'
)
 AND job_id > 'IT_PROG';
=============================
MIN替代ALL
SELECT employee_id AS 員工號(hào),
CONCAT(last_name,first_name) AS 姓名,
job_id AS 工種編號(hào),
salary AS 工資
FROM employees
WHERE salary(
  SELECT MIN(salary)
  FROM employees
  WHERE job_id='IT_PROG'
)
 AND job_id > 'IT_PROG';

#3.行子查詢(結(jié)果集一行多列或者多行多列)
#案例1.查詢員工編號(hào)最小并且工資最高的員工信息.引入
SELECT MIN(employee_id)
FROM employees;
=================
SELECT MAX(salary)
FROM employees;

SELECT *
FROM employees
WHERE employee_id = (
 SELECT MIN(employee_id)
 FROM employees
)
AND salary = (
 SELECT MAX(salary)
 FROM employees
     );

這種查詢結(jié)果使用虛擬字段,單行操作符必須一致可以使用。查出來與上面同樣的效果。
SELECT *
FROM employees
WHERE (employee_id,salary)=(
 SELECT MIN(employee_id),
 MAX(salary)
 FROM employees
       );
#二.SELECT子查詢
#僅僅支持標(biāo)量子查詢,結(jié)果是一行一列
#案例1.查詢每個(gè)部門的員工個(gè)數(shù)
SELECT d.*,(SELECT COUNT(*) FROM employees)
FROM departments d; 

添加條件
SELECT d.*,(SELECT COUNT(*)
FROM employees e
WHERE e.department_id=d.department_id
) AS 個(gè)數(shù)
FROM departments d; 

#案例2.查詢員工號(hào)=102的部門名。
SELECT department_name
FROM departments;
==============
SELECT employee_id
FROM employees
WHERE employee_id = 102;

SELECT employee_id,
(
 SELECT department_name
 FROM departments d
 WHERE e.department_id=d.department_id
)
FROM employees e
WHERE employee_id=102;

#三.FROM 后面
注意:將子查詢結(jié)果充當(dāng)一張表,要求必須起別名
#案例:查詢每個(gè)部門的平均工資等級(jí)。
SELECT ROUND(AVG(salary),2),department_id
FROM employees
GROUP BY department_id;

SELECT e.平均工資,j.grade_level
FROM job_grades AS j
,(
SELECT ROUND(AVG(salary),2) AS 平均工資,department_id
 FROM employees
 GROUP BY department_id
) AS e
WHERE e.平均工資 BETWEEN j.lowest_sal AND j.highest_sal;
#1999語法,老師答案
SELECT e.*,j.grade_level
FROM (
  SELECT ROUND(AVG(salary),2) AS 平均工資,department_id
  FROM employees
  GROUP BY department_id
) AS e
INNER JOIN job_grades j
ON e.平均工資 BETWEEN j.lowest_sal AND j.highest_sal;

#四.EXISTS后面(相關(guān)子查詢)
語法:EXISTS(完整的查詢語句)
備注:完整的查詢語句可以是一行一列,可以使一行多列
注意:先走外查詢,然后根據(jù)某個(gè)字段的值再去過濾
EXISTS 判斷(布爾類型)值存不存在,結(jié)果只有兩種:1有,0沒有
#引入
SELECT EXISTS(SELECT employee_id FROM employees);

查詢工資3W的員工信息
SELECT EXISTS(SELECT * FROM employees WHERE salary=30000);

#案例引入.查詢員工名和部門名
#查員工名與部門編號(hào)
SELECT first_name,department_id
FROM employees
WHERE department_id;

#查部門名
SELECT department_name
FROM departments;

#查員工名與部門名
SELECT e.first_name,d.department_name
FROM employees e
INNER JOIN (  SELECT department_name,department_id
 FROM departments
) AS d
ON e.department_id=d.department_id;

#案例1..查有員工的部門名
SELECT department_name
FROM departments d
WHERE EXISTS(
 SELECT *
 FROM employees e
 WHERE d.department_id=e.department_id
    );

使用IN代替EXISTS,同樣是上面的結(jié)果
SELECT department_name
FROM departments d
WHERE d.department_id IN(
 SELECT department_id
 FROM employees
     );
#案例2.查詢沒有女朋友的男神信息
#IN方法
SELECT *
FROM boys bo
WHERE bo.id NOT IN(
 SELECT boyfriend_id
 FROM beauty be
);
===============
#EXISTS方法
SELECT *
FROM boys bo
WHERE NOT EXISTS(
 SELECT boyfriend_id
 FROM beauty be
 WHERE bo.id=be.boyfriend_id
);

進(jìn)階9:聯(lián)合查詢
UNION 聯(lián)合 合并:將多條查詢語句的結(jié)果合并成一個(gè)結(jié)果。
語法:
 查詢語句1
 UNION
 查詢語句2
 UNION
 ...

應(yīng)用場景:
要查詢的結(jié)果來自于多個(gè)表,且多個(gè)表沒有直接的連接關(guān)系,
但查詢信息一致時(shí)。
網(wǎng)頁搜索內(nèi)容,內(nèi)容從不同的表中檢索聯(lián)合起來返回給用戶。

特點(diǎn):
1.要求多條查詢語句的查詢列數(shù)是一致的。
2.要求多條查詢語句的查詢的每一列的類型和順序最好一致。
3.使用UNION關(guān)鍵字默認(rèn)去重,如果使用UNION ALL全部展示,包含重復(fù)項(xiàng)

感謝能認(rèn)真讀到這里的伙伴們,MySql查詢部分結(jié)束,相信屏幕前的你照著我博客里的模板可以完成一些簡單的SQL查詢語句,SQL既然學(xué)了,以后還是要多練習(xí)一下,SQL1992與1999語法在主流的關(guān)系型數(shù)據(jù)庫都是通用的。后續(xù)我會(huì)繼續(xù)進(jìn)行對(duì)MySql的知識(shí)進(jìn)行擴(kuò)展,感興趣的同志互相關(guān)注一唄!o(^▽^)o

到此這篇關(guān)于MySql中子查詢內(nèi)查詢示例詳解的文章就介紹到這了,更多相關(guān)MySql 子查詢內(nèi)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySQL之select in 子查詢優(yōu)化的實(shí)現(xiàn)
  • mysql連接查詢、聯(lián)合查詢、子查詢?cè)砼c用法實(shí)例詳解
  • MySQL多表查詢實(shí)例詳解【鏈接查詢、子查詢等】
  • mysql實(shí)現(xiàn)多表關(guān)聯(lián)統(tǒng)計(jì)(子查詢統(tǒng)計(jì))示例
  • 詳解MySQL子查詢(嵌套查詢)、聯(lián)結(jié)表、組合查詢
  • MySQL 分組查詢和聚合函數(shù)
  • MySql中流程控制函數(shù)/統(tǒng)計(jì)函數(shù)/分組查詢用法解析
  • MySQL分組查詢Group By實(shí)現(xiàn)原理詳解
  • 詳解MySQL中的分組查詢與連接查詢語句
  • MySQL 子查詢和分組查詢

標(biāo)簽:鎮(zhèn)江 阜新 貴州 北京 鷹潭 合肥 日照 臺(tái)灣

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySql中子查詢內(nèi)查詢示例詳解》,本文關(guān)鍵詞  MySql,中子,查詢,內(nèi),示例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySql中子查詢內(nèi)查詢示例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySql中子查詢內(nèi)查詢示例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章