在 JDBC 编程中在另一个查询中使用一个查询

发布于 2024-10-05 03:46:55 字数 1058 浏览 5 评论 0原文

我了解如何在 SQL 中在纸面上执行此操作,但在 Java 中实现此操作时遇到困难(这是我第一次真正编写 JDBC 内容)

例如,假设我的数据库包含:

movie(code,标题,出版商)

客户(custno,名称)

借用(custno代码

我想找到借用的客户的姓名出版商 ABC 的每部电影

string no_of_ABC_movies = "SELECT COUNT(publisher), publisher FROM movie, WHERE movie.publisher = 'ABC'";

string no_of_cust_ABC_movies = "SELECT COUNT(name), name FROM customer, borrowed, movie, WHERE customer.custno = borrowed.custno AND borrowed.code = movie.code AND movie.publisher = 'ABC'";


String query = "SELECT name" +
                        " name FROM customer, borrowed, movie" +
                        " WHERE customer.custno = borrowed.custno AND" +
                        " borrowed.code = movie.code AND" +
                        " movie.publisher = 'ABC' AND" + " "
                         no_of_cust_ABC_movies + " = " + no_of_ABC_movies;

这不是我正在使用的确切数据库,但查询将起作用并打印出从 ABC 借用电影的人的姓名,但没有最后一行,但说我在最后一行的 SQL 语法中有错误行所以我想我不知道如何在另一个查询中使用一个查询。

I understand how to do this on paper in SQL, but am having trouble implementing this in Java (this is the first time I am actually programming JDBC stuff)

For example, say my database consists of:

movie(code, title, publisher)

customer(custno, name)

borrowed(custno, code)

And I want to find the name of customers who borrowed every movie by pubisher ABC

string no_of_ABC_movies = "SELECT COUNT(publisher), publisher FROM movie, WHERE movie.publisher = 'ABC'";

string no_of_cust_ABC_movies = "SELECT COUNT(name), name FROM customer, borrowed, movie, WHERE customer.custno = borrowed.custno AND borrowed.code = movie.code AND movie.publisher = 'ABC'";


String query = "SELECT name" +
                        " name FROM customer, borrowed, movie" +
                        " WHERE customer.custno = borrowed.custno AND" +
                        " borrowed.code = movie.code AND" +
                        " movie.publisher = 'ABC' AND" + " "
                         no_of_cust_ABC_movies + " = " + no_of_ABC_movies;

This isn't the exact database I am working with, but query will work and print out the names of people who borrowed movies from ABC without the last line, but says I have an error in SQL syntax with the last line so I guess I don't know how to use one query within another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一页 2024-10-12 03:46:55

这取决于您的 DBMS,但我见过的每个 SQL 变体都需要子查询周围的括号。

尝试类似的方法:

...

" movie.publisher = 'ABC' AND ("
no_of_cust_ABC_movies + ") = (" + no_of_ABC_movies + ")";

It depends on your DBMS, but every SQL variant I've seen requires parens around subqueries.

Try something like:

...

" movie.publisher = 'ABC' AND ("
no_of_cust_ABC_movies + ") = (" + no_of_ABC_movies + ")";
微凉徒眸意 2024-10-12 03:46:55

您在查询中遇到双名称字段没有用逗号分隔的问题。

如果您的代码与上面列出的完全相同,则在连接字符串的最后一行缺少 + 的上方会出现编译错误。

如果这是一个错字,下面是我的建议。

  1. 删除重复的选择(仅使用一个名称)或
  2. 用逗号分隔名称(尽管我没有看到选择名称两次的点)

并且您的最后一行是错误的..您不能以这种方式比较两个选择查询..只需添加所需的 where 子句。

(您应该先阅读数据库连接,然后再解决您的问题)

You have problem with double name field without being separated by a comma in your query.

If your code is exactly as listed above, you have compilation error just above the last line-missing + to concatenate strings.

If that's a typo below is my suggestion.

  1. Remove duplicate select (use only one name) or
  2. Separate names by a comma ( I don't see a point of selecting name twice though)

And your last line is wrong.. you can not compare two select queries that way.. Just add the required where clauses.

(You should read database joins first, and then solve your problem)

七堇年 2024-10-12 03:46:55

我喜欢让查询在查询浏览器或工作台中运行,然后将它们复制到 Java。它一次只保留一件新事物......

I like to get my queries working in the query browser or workbench, then copy them over to Java. It keeps it to one new thing at a time...

如梦亦如幻 2024-10-12 03:46:55

您的查询实际上以

 SELECT name name FROM customer ...

name 列重复开始 - 也许就是问题所在。

You're query actually starts with

 SELECT name name FROM customer ...

The name column is duplicated - maybe that the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文