JAVA程序设计(20)-----查询信息的数据库代码
增删改查 据说查询是最困难的……各种组合查询 联表查询 #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;

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
