gpt4 book ai didi

java - java中的嵌套选择

转载 作者:行者123 更新时间:2023-12-02 11:18:37 26 4
gpt4 key购买 nike

我想将下面代码中的 2 个选择合并到具有嵌套选择的单个查询中。我怎样才能实现这一目标?

这是我的代码

  int idst1 = // some Integer value

String SQLST1 = "select idl from " +
"studentlesson " +
"WHERE id=" + idst1;

rs = stmt.executeQuery(SQLST1);
rs.first();
id1 = rs.getInt(1);

String curses = "SELECT namel FROM lesson where idl=" + id1;
rs = stmt1.executeQuery(curses);
rs.first();

System.out.println("nxt:" + rs.next());
String s = rs.getString(7);
System.out.println(idst1 + ", " + s + ", ");

最佳答案

假设我正确理解并且您的表格如下:

LESSON
------
name1
id1

STUDENTLESSON
-------------
id
id1

其中 LESSON.id1STUDENTLESSON.id1

您可以使用以下查询:

SELECT name1 FROM LESSON, STUDENTLESSON
WHERE LESSON.id1 = STUDENTLESSON.id1
AND STUDENTLESSON.id = idst1

或者写成正确的 JOIN:

SELECT LESSON.name1
FROM LESSON
INNER JOIN STUDENTLESSON ON LESSON.id1=STUDENTLESSON.id1
WHERE id1 = idst1;

关于java - java中的嵌套选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50083387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com