简体   繁体   English

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

[英]Using one query in another in JDBC programming

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) 我了解如何在纸上使用SQL进行此操作,但是在用Java实现该功能时遇到了麻烦(这是我第一次真正编程JDBC东西)

For example, say my database consists of: 例如,假设我的数据库包括:

movie( code , title, publisher) 电影( 代码 ,标题,出版商)

customer( custno , name) 客户(custno,名)

borrowed( custno , code ) 借用( custnocode

And I want to find the name of customers who borrowed every movie by pubisher ABC 我想找到借出版商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. 这不是我正在使用的确切数据库,但是查询将起作用并打印出从ABC借来电影但没有最后一行的人的姓名,但是说我在最后一行中的SQL语法有误,所以我想我不知道如何在另一个查询中使用一个查询。

It depends on your DBMS, but every SQL variant I've seen requires parens around subqueries. 这取决于您的DBMS,但是我见过的每个SQL变体都需要对子查询进行绕行。

Try something like: 尝试类似:

...

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

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. 而且您的最后一行是错误的..您不能以这种方式比较两个选择查询。.仅添加所需的where子句。

(You should read database joins first, and then solve your problem) (您应该先阅读数据库联接,然后再解决问题)

I like to get my queries working in the query browser or workbench, then copy them over to Java. 我想让查询在查询浏览器或工作台中正常工作,然后将其复制到Java中。 It keeps it to one new thing at a time... 一次可以将它保持在一件新事物上...

You're query actually starts with 您要查询的实际上是从

 SELECT name name FROM customer ...

The name column is duplicated - maybe that the problem. name列重复-可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM