如何在Oracle中从时间戳类型的字段中选取特定日期

rkue9o1l  于 2022-12-11  发布在  Oracle
关注(0)|答案(1)|浏览(163)

嗨,我正在尝试创建一个查询,将在特定日期在oracle中销售的平板电脑。字段数据类型是时间戳。
下面是查询

SELECT 
  Order_detail.order_detail_id,
  product_name AS product,
  Product_categ_type.product_categ_type AS category,
  Order_line.qty AS qty,
  order_date
FROM Product, Order_detail, Order_line, Product_categ_type
WHERE Order_detail.order_detail_id = Order_line.order_detail_id
  AND Product.product_id = Order_line.product_id
  AND Product.product_categ_type_id = 3
  AND Product_categ_type.product_categ_type LIKE 'TB%'
  AND order_date = TO_DATE('2022-12-02' ,'yyyy,mm,dd')

查询可以工作,但不返回任何数据,我认为问题出在如何选择日期上

-- INSERT INTO PRODUCT TABLE
INSERT INTO Product (product_id,product_categ_type_id,product_name,price,stock_qty) VALUES (174,1,'iWatch',454,183);

INSERT INTO Product (product_id,product_categ_type_id,product_name,price,stock_qty) VALUES (194,3,'Samsung Galaxy Tab',398,114);

INSERT INTO Product (product_id,product_categ_type_id,product_name,price,stock_qty) VALUES (139,1,'Fitness Tracker',312,122);

INSERT INTO Product (product_id,product_categ_type_id,product_name,price,stock_qty) VALUES (150,3,'iPad',366,189);

-- INSERT INTO PRODUCT CATEGORY TABLE

INSERT INTO Product_categ_type (product_categ_type_id,product_categ_type) VALUES (1,'AC');
INSERT INTO Product_categ_type (product_categ_type_id,product_categ_type) VALUES (2,'SP');
INSERT INTO Product_categ_type (product_categ_type_id,product_categ_type) VALUES (3,'TB');

-- INSERT INTO Order detail

INSERT INTO Order_detail (order_detail_id,product_id,customer_id,emp_id) VALUES (144,150,196,113);
INSERT INTO Order_detail (order_detail_id,product_id,customer_id,emp_id) VALUES (183,139,140,197);
INSERT INTO Order_detail (order_detail_id,product_id,customer_id,emp_id) VALUES (172,194,185,113);

-- INSERT INTO Order line

INSERT INTO Order_line (product_id,order_detail_id,qty) VALUES (150,144,1);
INSERT INTO Order_line (product_id,order_detail_id,qty) VALUES (139,183,5);
INSERT INTO Order_line (product_id,order_detail_id,qty) VALUES (194,172,1);
7gcisfzg

7gcisfzg1#

使用24小时范围:

AND order_date >= DATE '2022-12-02'
AND order_date <  DATE '2022-12-03'

或者,您可以使用TRUNC将列截断到同一天得午夜,这样就不必担心时间部分:

AND TRUNC(order_date) = DATE '2022-12-02'

但是,在这种情况下,Oracle将无法在order_date列上使用任何索引,而需要在TRUNC(order_date)上使用基于函数的索引,因此通常最好使用范围。

相关问题