会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132648个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1398楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1399楼
JAVA 全系列/第三阶段:数据库编程/数据库设计范式 1400楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 1401楼

查找各部门薪资最低的雇员,并显示雇员的名字,薪水,部门ID。

这一题视频中的解答有问题。各部门薪水最低的雇员的薪资应该是唯一的,但是视频中解答的SQL的输出结果如下:

SQL> select last_name,salary,department_id from employees where salary in (select min(salary) 
from employees group by department_id) order by department_id;
LAST_NAME                     SALARY DEPARTMENT_ID
------------------------- ---------- -------------
Whalen                       4400.00            10
Fay                          6000.00            20
Colmenares                   2500.00            30
Mavris                       6500.00            40
Olson                        2100.00            50
Patel                        2500.00            50
Vargas                       2500.00            50
Marlow                       2500.00            50
Vollman                      6500.00            50
Perkins                      2500.00            50
Sarchand                     4200.00            50
Sullivan                     2500.00            50
Lorentz                      4200.00            60
Ernst                        6000.00            60
Baer                        10000.00            70
Kumar                        6100.00            80
Bloom                       10000.00            80
Sewall                       7000.00            80
King                        10000.00            80
Tucker                      10000.00            80
LAST_NAME                     SALARY DEPARTMENT_ID
------------------------- ---------- -------------
Tuvault                      7000.00            80
De Haan                     17000.00            90
Kochhar                     17000.00            90
Popp                         6900.00           100
Gietz                        8300.00           110
Grant                        7000.00 
26 rows selected

以部门50为例有多个最低薪资,这个不符合逻辑。


我觉得这里要使用到关联子查询,如下:

SQL> select e1.last_name,e1.salary,e1.department_id from employees e1 where e1.department_id in(
  2      select distinct e2.department_id from employees e2
  3  ) and e1.salary=(
  4      select min(e3.salary) from employees e3 where e3.department_id=e1.department_id
  5  ) order by e1.department_id;
LAST_NAME                     SALARY DEPARTMENT_ID
------------------------- ---------- -------------
Whalen                       4400.00            10
Fay                          6000.00            20
Colmenares                   2500.00            30
Mavris                       6500.00            40
Olson                        2100.00            50
Lorentz                      4200.00            60
Baer                        10000.00            70
Kumar                        6100.00            80
De Haan                     17000.00            90
Kochhar                     17000.00            90
Popp                         6900.00           100
Gietz                        8300.00           110
12 rows selected

其中部门90出现两次是因为两人并列最低部门薪资。

SQL> select last_name,salary,department_id from employees where department_id=90;
LAST_NAME                     SALARY DEPARTMENT_ID
------------------------- ---------- -------------
King                        24000.00            90
Kochhar                     17000.00            90
De Haan                     17000.00            90


JAVA 全系列/第三阶段:数据库编程/SQL 语言 1402楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1403楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1404楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1406楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 1408楼

问题:老师用Oracle进行操作时,应该代码没有问题,因为插入操作能执行成功,但是遇到的问题是进行删除操作,console空值台什么也不显示,删除也失败了,再进行插入操作,此时插入操作也不成功,但console什么也不显示,(重启eclipse时,插入操作能够完成,但是一执行删除操作,则所有操作全不能进行了)这是什么原因?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo {
 private static final String URL= "jdbc:oracle:thin:@localhost:1521:ORCL";
 private static final String USERNAME ="BJSXT";
 private static final String PWD = "oracle"; 
 //实现增删改查
 public static void update() throws SQLException {
  Connection conn=null;
  Statement state=null;
  //a.导入驱动,加载具体的驱动类
  try {
   Class.forName("oracle.jdbc.OracleDriver");
   //b.与数据库进行连接
   conn = DriverManager.getConnection(URL,USERNAME,PWD);
   //发送sql,执行(增删改,查)
   state = conn.createStatement();
   //插入
   String sql="insert into T_USERS values(8,'WangYaXing',25)";
   
   //修改数据
   //String sql = "update T_USERS set name='李四' where id = 1;";
   
   //删除数据
  // String sql = "delete from T_USERS where name='WangYaXin'";
   
   //执行sql
   int count = state.executeUpdate(sql);
   //处理结果
   if(count>0) {
    System.out.println("操作成功!");
   }
   
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch(SQLException e) {
   e.printStackTrace();
  }finally {
   try {
    if(state!=null) {
     state.close(); //对象为空
    }
    if(conn!=null) {
     conn.close();
    }
    
   } catch (SQLException e) {
    // TODO: handle exception
    e.printStackTrace();
   }
  }
 }
 
 public static void main(String[] args) throws SQLException {
  update();
 }
}


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1410楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637