在 JDBC 编程中在另一个查询中使用一个查询
我了解如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这取决于您的 DBMS,但我见过的每个 SQL 变体都需要子查询周围的括号。
尝试类似的方法:
It depends on your DBMS, but every SQL variant I've seen requires parens around subqueries.
Try something like:
您在查询中遇到双名称字段没有用逗号分隔的问题。
如果您的代码与上面列出的完全相同,则在连接字符串的最后一行缺少 + 的上方会出现编译错误。
如果这是一个错字,下面是我的建议。
并且您的最后一行是错误的..您不能以这种方式比较两个选择查询..只需添加所需的 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.
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)
我喜欢让查询在查询浏览器或工作台中运行,然后将它们复制到 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...
您的查询实际上以
name
列重复开始 - 也许就是问题所在。You're query actually starts with
The
name
column is duplicated - maybe that the problem.