Oracle 最小和最大列查询

发布于 2024-12-06 02:29:09 字数 870 浏览 2 评论 0原文

好吧,需要一些帮助来创建 Oracle 10g DB 的查询。我有一个表如下所示:

----------------------------------------
| lowerBound | upperBound | locationId |
----------------------------------------
|      0     |     99     |     1      |
----------------------------------------
|     100    |    199     |     2      |
----------------------------------------
...

另一个表如下所示:

-----------------------------
| locationId | locationCode |
-----------------------------
|     1      |     12345    |
-----------------------------
|     2      |     23456    |
-----------------------------
|     3      |     34567    |
-----------------------------
...

我以一个数字开头,比如 113,但它是在 java 中计算出来的变量。我需要根据它落在 lowerBound 和 upperBound 列之间找出与该数字对应的 locationId,然后将其加入以找出第二个表中的 locationCode。我查过诸如 MIN/MAX 之类的东西,但是我没有找到我正在寻找的东西。我不是一个好的 DBA,因此感谢您的帮助。

Alright, need some help created a query for an Oracle 10g DB. I have a table to looks like this:

----------------------------------------
| lowerBound | upperBound | locationId |
----------------------------------------
|      0     |     99     |     1      |
----------------------------------------
|     100    |    199     |     2      |
----------------------------------------
...

Another table looks like this:

-----------------------------
| locationId | locationCode |
-----------------------------
|     1      |     12345    |
-----------------------------
|     2      |     23456    |
-----------------------------
|     3      |     34567    |
-----------------------------
...

I start with a number, say 113, but it is a variable figured out in java. I need to figure out the locationId that corresponds to that number, based on it falling between the lowerBound and upperBound columns, and then join that to figure out the locationCode in the 2nd table. I've looked up things like MIN/MAX and between, however I am not finding exactly what I am looking for. I am not a good DBA, so any help is appreciated.

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

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

发布评论

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

评论(1

自由如风 2024-12-13 02:29:09
SELECT t2.locationCode
  FROM table1 t1
 INNER JOIN table2 t2 USING(locationId)
 WHERE 113 BETWEEN t1.lowerBound AND t1.upperBound

如果 USING 部分让您感到困惑:

SELECT t2.locationCode
  FROM table1 t1
 INNER JOIN table2 t2 ON t2.locationId = t1.locationId
 WHERE 113 BETWEEN t1.lowerBound AND t1.upperBound

请注意,如果您的 lowerBound 和 upperBound 列是整数,则这将起作用。如果它们是 VARCHAR,那么您当然必须使用 TO_NUMBER 函数。

SELECT t2.locationCode
  FROM table1 t1
 INNER JOIN table2 t2 USING(locationId)
 WHERE 113 BETWEEN t1.lowerBound AND t1.upperBound

If the USING part puzzles you:

SELECT t2.locationCode
  FROM table1 t1
 INNER JOIN table2 t2 ON t2.locationId = t1.locationId
 WHERE 113 BETWEEN t1.lowerBound AND t1.upperBound

Note that this will work if your lowerBound and upperBound columns are INTEGERS. If they are VARCHAR, you will have to use the TO_NUMBER function of course.

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