如何从数组中选择项目(IN 子句详细信息)?
我想用 Java 做一些事情(使用 iBatis、JDBC 等,实际上是在 SQL 中),例如:
SELECT SUM(rowName) FROM myTable WHERE id = [myArrayOfIds]
其中 myArrayOfIds 几乎可以是任何长度。现在我知道你可以这样做:
SELECT SUM(rowName) FROM myTable WHERE id IN (x, y, z)
但是对于更长的列表会发生什么?例如,我的列表可以少至几个项目,也可以少至数百个或更多项目。我该怎么做?
I would like to do something in Java (using iBatis, JDBC, etc., really in SQL) like:
SELECT SUM(rowName) FROM myTable WHERE id = [myArrayOfIds]
Where myArrayOfIds can be almost any length. Now I know you can do:
SELECT SUM(rowName) FROM myTable WHERE id IN (x, y, z)
but what happens for longer lists? For example my list could be as little as a few items to hundreds or more items. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这取决于您对 SQL 的风格。例如,Oracle 不允许
IN()
列表中的值超过 1000 个。其他口味可能会有所不同。I think it depends on your flavour of SQL. For instance, Oracle does not allow more than 1000 values in an
IN()
list. Other flavours may vary.一种替代方法是将这些 id 插入表中,然后进行连接
one alternative would be to insert those ids to a table, then do a join
Oracle 绝对允许
IN
子句中包含超过 1000 个项目。这是你的持久性工具限制了这一点。iBatis
或 Hibernate,等等。使用 Oracle Sqlplus,您会发现这不是 Oracle 的限制。BlackTigerX 的建议 可行,或者您可以多次调用查询,一次传递 1000 个项目并聚合结果。无论哪种方式,您都只是在解决持久性工具的限制。
Oracle definitely allows more than 1000 items in the
IN
clause. It's your persistence tool that is limiting this.iBatis
or Hibernate, whatever. Use Oracle Sqlplus and you'll see this is not an Oracle limit.Suggestion from BlackTigerX would work, or you could call the query multiple times, passing 1000 items at a time and aggregating the results. Either way, you're just working around your persistence tool limitation.