(SQL)我必须使用IN运算符从2个不同表中获取信息

发布于 2025-02-06 01:53:13 字数 628 浏览 3 评论 0原文

这是我学习SQL的第一年,所以请忍受:)

我给出的任务中的一个问题是问我:“使用操作员在2015年10月12日下达订单的每个客户的号码和名称。 “因此,这是我尝试使用的方法:

SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE ORDER_DATE IN
(SELECT ORDER_DATE
FROM ORDERS
WHERE ORDER_DATE = '12-OCT-15');

当我运行此代码时,我会有一个错误:

ora-00904:“ order_date”:无效标识符

  1. 00000-“%s:无效标识符

我查看了我的教授在笔记中提供的示例,并且我已经尝试了周围的内容(这只是给出了关于文字的错误)。我需要来自两个不同表的数据,因为customer_num和customer_name在客户表中,订单_date位于订单表中(customer_num也在订单表中),

感谢您的任何帮助。 抱歉,如果这是一个愚蠢的问题,我只是不知道该怎么办::)

让我知道我是否需要从我使用的数据中添加更多信息,以便您可以帮助我。

this is my first year learning SQL so bear with me :)

A question in an assignment I was given is asking me this: "Use the IN operator to find the number and name of each customer that placed an order on October 12, 2015." So, here's what I tried using:

SELECT CUSTOMER_NUM, CUSTOMER_NAME
FROM CUSTOMER
WHERE ORDER_DATE IN
(SELECT ORDER_DATE
FROM ORDERS
WHERE ORDER_DATE = '12-OCT-15');

When I ran this code, I got an error:

ORA-00904: "ORDER_DATE": invalid identifier

  1. 00000 - "%s: invalid identifier

I've looked at the examples my professor provided in our notes and I've tried changing things around (that just gave an error about Literals). I need data from two different tables as CUSTOMER_NUM and CUSTOMER_NAME are in the Customer table and ORDER_DATE is in the Orders table (CUSTOMER_NUM is also in the Orders table)

Thanks for any help.
Sorry if this is a dumb question, I just don't know what to do from here :)

Let me know if I need to add more information from the data I'm using so you can help me.

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

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

发布评论

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

评论(3

娇妻 2025-02-13 01:53:14
SELECT DISTINCT CUSTOMER.CUSTOMER_NUM, 
       CUSTOMER.CUSTOMER_NAME
FROM CUSTOMER
INNER JOIN ORDERS ON CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
WHERE ORDER.ORDER_DATE IN ('2015-10-12');

我习惯于postgresql,所以我不确定该语法是否可以与Oracle一起使用,但您明白了。

SELECT DISTINCT CUSTOMER.CUSTOMER_NUM, 
       CUSTOMER.CUSTOMER_NAME
FROM CUSTOMER
INNER JOIN ORDERS ON CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
WHERE ORDER.ORDER_DATE IN ('2015-10-12');

I'm used to POSTGRESQL so i'm not sure if this syntax will work with oracle but you get the point.

长不大的小祸害 2025-02-13 01:53:13

从客户('12 -oct-15')中的order_date中选择customer_num,customer_name。
尝试一下

SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE ORDER_DATE IN ('12-OCT-15');
Try this

假装爱人 2025-02-13 01:53:13

It would seem the initial error stems from where order_date in predicate, as I would guess (even without table description) order_date does not exist in the customer表。由于需要使用IN,因此似乎初始任务是从订单表中找到customer_num,然后访问客户表以获取所找到的值。 未测试

select cust.customer_num, 
       cust.customer_name
 from customer cust
where cust.customer_num in 
      (select ord.customer_num 
         from orders ord
        where trunc(ord.order_date) = date '2015-08-12'
      );

注意:

  • 使用语法order_date('12 -oct-15')是不好的做法
    取决于隐式转换和NLS_DATE_FORMAT的设置。而是使用显式转换日期2015-08-12(ISO日期
    规格)。

  • 在Oracle中,所有日期数据类型都包含第二个时间组件。
    您使用trunc(< date_column>)剥离时间(实际设置
    到00:00:00)。显式转换也将设定时间相同。
    然后允许准确的日期=日期比较。

  • 不需要独特。子查询可以返回重复项
    客户数字,但外部查询将停止寻找匹配
    当它击中第一个时,所以长customer_num是唯一的
    您将不会获得重复的客户表。

It would seem the initial error stems from where order_date in predicate, as I would guess (even without table description) order_date does not exist in the customer table. Since use of IN is required it seems initial task is finding customer_num from the order table then accessing the customer table for the values found. Not tested.

select cust.customer_num, 
       cust.customer_name
 from customer cust
where cust.customer_num in 
      (select ord.customer_num 
         from orders ord
        where trunc(ord.order_date) = date '2015-08-12'
      );

Notes:

  • Using the syntax order_date in ('12-OCT-15') is bad practice as it
    depends on implicit conversion and the setting of NLS_DATE_FORMAT. Instead employ explicit conversion date 2015-08-12 (ISO date
    specification).

  • In Oracle all date data types contain time components to the second.
    You use trunc(<date_column>) to strip off the time (actually set it
    to 00:00:00). The explicit conversion will also set time the same.
    This then allows accurate date=date comparison.

  • DISTINCT is not required. The sub-query may return duplicates
    customers numbers, but the outer query will stop looking for a match
    when it hits the first one, so as long customer_num is unique in
    the customer table you will not get duplicates.

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