(SQL)我必须使用IN运算符从2个不同表中获取信息
这是我学习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”:无效标识符
- 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
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我习惯于postgresql,所以我不确定该语法是否可以与Oracle一起使用,但您明白了。
I'm used to POSTGRESQL so i'm not sure if this syntax will work with oracle but you get the point.
从客户('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
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 thecustomer
表。由于需要使用IN,因此似乎初始任务是从订单表中找到customer_num
,然后访问客户表以获取所找到的值。 未测试。注意:
使用语法
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 thecustomer
table. Since use of IN is required it seems initial task is findingcustomer_num
from the order table then accessing the customer table for the values found. Not tested.Notes:
Using the syntax
order_date in ('12-OCT-15')
is bad practice as itdepends on implicit conversion and the setting of NLS_DATE_FORMAT. Instead employ explicit conversion
date 2015-08-12
(ISO datespecification).
In Oracle all date data types contain time components to the second.
You use
trunc(<date_column>)
to strip off the time (actually set itto 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 inthe customer table you will not get duplicates.