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

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

Jun 07, 2016 pm 04:13 PM
java code information delete database Inquire programming

增删改查 据说查询是最困难的……各种组合查询 联表查询 #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;
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

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

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles