你怎么能写这个查询?

发布于 2024-09-24 06:00:26 字数 1132 浏览 3 评论 0原文

我有两张桌子。 表的结构如下。

TRAILERS_INVENTORY

  • TRAILER_TYPE VARCHAR2(100)
  • TRAILER_LENGTH INT
  • TRAILER_WIDTH INT
  • YEAR_OF_MANUFACTURE INT

NEW_INVENTORY

  • NEW_INVENTORY_TYPE_ID INT、
  • TRAILER_TYPE VARCHAR2(100)、
  • TRAILER_LENGTH INT、
  • TRAILER_WIDTH INT、
  • UFACTURE INT

我想列出所有没有 NEW_INVENTORY_TYPE_ID 的预告片。我使用的是Oracle 9.2。

一种方法是使用集合运算,类似于

SELECT TRAILER_TYPE
 FROM TRAILERS_INVENTORY 
WHERE TRAILER_TYPE NOT IN (SELECT TRAILER_TYPE, 
                                  TRAILER_LENGTH, 
                                  TRAILER_WIDTH, 
                                  YEAR_OF_MANUFACTURE 
                             FROM TRAILERS_INVENTORY 
                           INTERSECT 
                           SELECT TRAILER_TYPE, 
                                  TRAILER_LENGTH, 
                                  TRAILER_WIDTH, 
                                  YEAR_OF_MANUFACTURE 
                             FROM NEW_INVENTORY);

还有其他可用的方法吗?

I have two tables.
The structure of tables is as follows.

TRAILERS_INVENTORY

  • TRAILER_TYPE VARCHAR2(100)
  • TRAILER_LENGTH INT
  • TRAILER_WIDTH INT
  • YEAR_OF_MANUFACTURE INT

NEW_INVENTORY

  • NEW_INVENTORY_TYPE_ID INT,
  • TRAILER_TYPE VARCHAR2(100),
  • TRAILER_LENGTH INT,
  • TRAILER_WIDTH INT,
  • YEAR_OF_MANUFACTURE INT

I want to list all of the trailers that do not have a NEW_INVENTORY_TYPE_ID. I am using Oracle 9.2.

One approach is to use a set operation along the lines of

SELECT TRAILER_TYPE
 FROM TRAILERS_INVENTORY 
WHERE TRAILER_TYPE NOT IN (SELECT TRAILER_TYPE, 
                                  TRAILER_LENGTH, 
                                  TRAILER_WIDTH, 
                                  YEAR_OF_MANUFACTURE 
                             FROM TRAILERS_INVENTORY 
                           INTERSECT 
                           SELECT TRAILER_TYPE, 
                                  TRAILER_LENGTH, 
                                  TRAILER_WIDTH, 
                                  YEAR_OF_MANUFACTURE 
                             FROM NEW_INVENTORY);

IS THERE ANOTHER APPROACH AVAILABLE?

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

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

发布评论

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

评论(3

柏拉图鍀咏恒 2024-10-01 06:00:26

我看到几个选项

SELECT
    TRAILER_TYPE
FROM
    TRAILERS_INVENTORY
WHERE
    TRAILER_TYPE NOT IN (SELECT TRAILER_TYPE FROM NEW_INVENTORY)

或者:

SELECT
    TRAILER_TYPE
FROM
    TRAILERS_INVENTORY
LEFT JOIN
    NEW_INVENTORY
    ON (NEW_INVENTORY.TRAILER_TYPE = TRAILERS_INVENTORY.TRAILER_TYPE)
WHERE
    NEW_INVENTORY.NEW_INVENTORY_TYPE_ID IS NULL

I see a couple of options

SELECT
    TRAILER_TYPE
FROM
    TRAILERS_INVENTORY
WHERE
    TRAILER_TYPE NOT IN (SELECT TRAILER_TYPE FROM NEW_INVENTORY)

Or:

SELECT
    TRAILER_TYPE
FROM
    TRAILERS_INVENTORY
LEFT JOIN
    NEW_INVENTORY
    ON (NEW_INVENTORY.TRAILER_TYPE = TRAILERS_INVENTORY.TRAILER_TYPE)
WHERE
    NEW_INVENTORY.NEW_INVENTORY_TYPE_ID IS NULL
如日中天 2024-10-01 06:00:26

那么,问题是如何在不使用“set”操作的情况下执行此查询?

请问为啥不用套装呢?

您在 SQL 中所做的一切都基于集合,但如果您想要一个更慢、不太优雅的解决方案,您可以将集合大小减少到 1 并使用 PL/SQL 循环遍历 TRAILERS_INVENTORY

declare
    v_exists varchar2(1 char);
begin
    for item in (select distinct trailer_type from trailers_inventory) loop
        begin
            select 'Y' into v_exists from new_inventory
            where trailer_type = item.trailer_type;
        exception
            when no_data_found then
                v_exists := null;
        end;
        if v_exists is null then
            dbms_output.put_line(item.trailer_type || ' not in ' || ' NEW_INVENTORY');
        end if;
    end loop;
end;
/

哎呀!这就是为什么集合操作是最佳选择。

:)

So, the question is how can you perform this query without using a "set" operation?

Can I ask why you wouldn't use a set?

Everything you do in SQL is based on sets, but if you want a slower, less elegant solution you can reduce your set size to 1 and loop through TRAILERS_INVENTORY using PL/SQL.

declare
    v_exists varchar2(1 char);
begin
    for item in (select distinct trailer_type from trailers_inventory) loop
        begin
            select 'Y' into v_exists from new_inventory
            where trailer_type = item.trailer_type;
        exception
            when no_data_found then
                v_exists := null;
        end;
        if v_exists is null then
            dbms_output.put_line(item.trailer_type || ' not in ' || ' NEW_INVENTORY');
        end if;
    end loop;
end;
/

Eek! That's why set operations are the way to go.

:)

陌生 2024-10-01 06:00:26

怎么样:

SELECT 
TRAILER_TYPE, 
TRAILER_LENGTH, 
TRAILER_WIDTH, 
YEAR_OF_MANUFACTURE 
FROM TRAILERS_INVENTORY 
MINUS
SELECT 
TRAILER_TYPE, 
TRAILER_LENGTH, 
TRAILER_WIDTH, 
YEAR_OF_MANUFACTURE 
FROM NEW_INVENTORY

What about:

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