首頁 資料庫 mysql教程 JAVA程序设计(20)-----查询信息的数据库代码

JAVA程序设计(20)-----查询信息的数据库代码

Jun 07, 2016 pm 04:13 PM
java 程式碼 資訊 資料庫 查詢 程式設計

增删改查 据说查询是最困难的……各种组合查询 联表查询 #0. 查询最高工资及其对应员工姓名select ename, sal from empwhere sal=(select max(sal) from emp);#如果有多个员工都是最高工资下面的方式将失效select ename, sal from emp ORDER BY sal desc lim

增删改查 据说查询是最困难的……各种组合查询 联表查询

#0. 查询最高工资及其对应员工姓名
select ename, sal from emp
where sal=(select max(sal) from emp);

#如果有多个员工都是最高工资下面的方式将失效
select ename, sal from emp ORDER BY sal desc limit 0, 1;

#补充1:能否不使用聚合函数查出最高工资及其对应员工姓名
select ename, sal from emp
where sal=(select sal from emp order by sal desc limit 0,1);

#补充2:既不用排序也不用聚合函数查出最高工资及其对应员工姓名
select ename, sal from emp
where sal not in 
(select distinct t1.sal from emp as t1
inner join emp as t2 on t1.sal<t2.sal);


#1. 计算每位员工的年薪
select ename as 姓名, (sal+if(comm is null, 0, comm))*12 as 年薪 
from emp order by 年薪 DESC;

#2. 统计有员工的部门的人数
select dname as 部门名称, 总人数 from
(select dno, count(dno) as 总人数 from emp
group by dno) as t1, dept as t2
where t1.dno=t2.dno;	#没有联接条件将产生笛卡尔积

SELECT dname as 部门名称, 总人数 FROM
(select dno, count(dno) as 总人数 from emp
group by dno) as t1 INNER JOIN dept as t2
on t1.dno=t2.dno;

#补充:把没有员工的部门也显示出来
SELECT dname as 部门名称, if(total is null, 0, total) as 总人数 
FROM (select dno, count(dno) as total from emp
group by dno) as t1 RIGHT JOIN dept as t2
on t1.dno=t2.dno;

SELECT dname as 部门名称, if(total is null, 0, total) as 总人数 
FROM dept as t2 LEFT JOIN 
(select dno, count(dno) as total from emp group by dno) as t1
on t1.dno=t2.dno;

#3.【本文来自鸿网互联 (http://www.68idc.cn)】 求挣最高薪水的员工(boss除外)的姓名
select ename, sal from emp
where sal=(select max(sal) from emp where mgr is not null);

#4. 查询薪水超过平均薪水的员工的姓名和工资
select ename, sal
from emp where sal>(select avg(sal) from emp);

#5. 查询薪水超过其所在部门平均薪水的员工的姓名、部门名称和工资
#where写法
select ename, dname, t3.sal from
(select eno, t1.dno, sal from emp as t1,
(select dno, avg(sal) as avgSal from emp group by dno) as t2
where t1.dno=t2.dno and sal>avgSal) as t3, emp as t4, dept as t5 
where t3.eno=t4.eno and t5.dno=t3.dno;

#inner join写法
select ename, dname, t3.sal from
(select eno, t1.dno, sal from emp as t1 inner join
(select dno, avg(sal) as avgSal from emp group by dno) as t2
on t1.dno=t2.dno and sal>avgSal) as t3 inner join 
emp as t4 on t3.eno=t4.eno inner join 
dept as t5 on t5.dno=t3.dno;

#6. 查询部门中薪水最高的人姓名、工资和所在部门名称
select ename, dname, t3.sal from
(select eno, t1.dno, sal from emp as t1 inner join
(select dno, max(sal) as maxSal from emp group by dno) as t2
on t1.dno=t2.dno and sal=maxSal) as t3 inner join 
emp as t4 on t3.eno=t4.eno inner join 
dept as t5 on t5.dno=t3.dno;

#7. 哪些人是主管
select * from emp 
where eno in 
(select distinct mgr from emp);

select * from emp 
where eno=any(select distinct mgr from emp);

#补充:哪些人不是主管
select * from emp 
where eno not in (select distinct mgr from emp where mgr is not null);

#8. 求平均薪水最高的部门的名称和平均工资
select dname as 部门名称, avgSal as 平均工资 from
(select dno, avgSal
from (select dno, avg(sal) as avgSal from emp
group by dno) t1 
where avgSal=(select max(avgSal) from 
(select dno, avg(sal) as avgSal from emp group by dno) as t2)) as t3
inner join dept as t4 on t3.dno=t4.dno;

#9. 求薪水最高的前3名雇员
select * from emp order by sal desc limit 0,3;

#10.求薪水排在第4-6名雇员
select * from emp order by sal desc limit 3,3;
登入後複製


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1425
52
Laravel 教程
1325
25
PHP教程
1272
29
C# 教程
1252
24
PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP與Python:核心功能 PHP與Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

MySQL:世界上最受歡迎的數據庫的簡介 MySQL:世界上最受歡迎的數據庫的簡介 Apr 12, 2025 am 12:18 AM

MySQL是一種開源的關係型數據庫管理系統,主要用於快速、可靠地存儲和檢索數據。其工作原理包括客戶端請求、查詢解析、執行查詢和返回結果。使用示例包括創建表、插入和查詢數據,以及高級功能如JOIN操作。常見錯誤涉及SQL語法、數據類型和權限問題,優化建議包括使用索引、優化查詢和分錶分區。

PHP:許多網站的基礎 PHP:許多網站的基礎 Apr 13, 2025 am 12:07 AM

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

PHP與Python:用例和應用程序 PHP與Python:用例和應用程序 Apr 17, 2025 am 12:23 AM

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

為什麼要使用mysql?利益和優勢 為什麼要使用mysql?利益和優勢 Apr 12, 2025 am 12:17 AM

選擇MySQL的原因是其性能、可靠性、易用性和社區支持。 1.MySQL提供高效的數據存儲和檢索功能,支持多種數據類型和高級查詢操作。 2.採用客戶端-服務器架構和多種存儲引擎,支持事務和查詢優化。 3.易於使用,支持多種操作系統和編程語言。 4.擁有強大的社區支持,提供豐富的資源和解決方案。

See all articles