如何在 Java 中使用带有 IN 的 sql 查询作为 select 语句

发布于 2024-11-07 05:33:28 字数 317 浏览 0 评论 0原文

我需要使用 java 中的 sql 查询,其中包含 IN 子句。我已经看到了当 IN 子句是预先确定的集合时执行此操作的建议,例如在这种情况下不起作用的数字,因为该集合是动态的。这就是我想做的:

select c.cust_name 
from cust_acct c, has h 
where c.cust_id=h.cust_id 
    and h.sh_add in (select a.sh_add from address a where sh_st='VA')

我没有遇到任何错误。也就是说,代码运行时我只是没有得到这个特定查询的结果

I need to us a sql query in java that has an IN clause in it. I have seen suggestions for doing this when the IN clause is a predetermined set, such as numbers which won't work in this case because the set will be dynamic. Here is what I am trying to do:

select c.cust_name 
from cust_acct c, has h 
where c.cust_id=h.cust_id 
    and h.sh_add in (select a.sh_add from address a where sh_st='VA')

I do not get any faults. That is, the code runs I just get no results for this particular query

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

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

发布评论

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

评论(1

盗梦空间 2024-11-14 05:33:28

您是否检查过从 select a.sh_add from address a where sh_st='VA' 获得了正确的中间结果?

您可以将查询重写为:

select c.cust_name 
from cust_acct c
inner join has h 
on c.cust_id=h.cust_id 
inner join address a
on h.sh_add = a.sh_add
where a.sh_st = 'VA'

Have you checked that you are getting the right intermediate results from select a.sh_add from address a where sh_st='VA'?

You could just rewrite the query as:

select c.cust_name 
from cust_acct c
inner join has h 
on c.cust_id=h.cust_id 
inner join address a
on h.sh_add = a.sh_add
where a.sh_st = 'VA'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文