SQL - 选择产品与最新开始日期之间的天数

时间:2016-04-05 05:03:49

标签: sql oracle date

解决

SELECT TO_DATE(PRODUCT_FINISH_DATE) - TO_DATE(PRODUCT_START_DATE)
FROM MANUFACTURED_PRODUCT
WHERE ROWNUM = 1
ORDER BY PRODUCT_START_DATE ASC;

-

表:MANUFACTURED_PRODUCT

列:PRODUCT_ID, PRODUCT_START_DATE, PRODUCT_FINISH_DATE

SS:http://i.imgur.com/L7BePwc.png

尝试1:

SELECT DATEDIFF(D, PRODUCT_START_DATE, PRODUCT_FINISH_DATE) as Difference
FROM MANUFACTURED_PRODUCT
ORDER BY PRODUCT_START_DATE ASC LIMIT 1;

错误1:

ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
Error at Line: 3 Column: 32

尝试2:

SELECT DATEDIFF(D, PRODUCT_START_DATE, PRODUCT_FINISH_DATE) as Difference
FROM MANUFACTURED_PRODUCT
ORDER BY PRODUCT_START_DATE ASC;

错误2:

ORA-00904: "DATEDIFF": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 1 Column: 7

我看到的所有例子都是这样的:

select DATEDIFF(d, @start, @end)
from table
order by @start asc limit 1;

3 个答案:

答案 0 :(得分:0)

我认为应该是

select datediff(PRODUCT_START_DATE,PRODUCT_FINISH_DATE) as Difference from FROM MANUFACTURED_PRODUCT
ORDER BY PRODUCT_START_DATE ASC;

答案 1 :(得分:0)

虽然您已将问题标记为mysql个问题,但您正在针对不支持oracleLIMIT的{​​{1}}数据库运行查询。

您的第一个错误是因为您使用无法识别的DateDiff结束了查询,而第二个错误是因为未知函数LIMIT

编辑:你可以使用这段代码:

DateDiff

答案 2 :(得分:0)

试试这个

SELECT TO_DATE(PRODUCT_FINISH_DATE,' dd / mm / yyyy') - TO_DATE(PRODUCT_START_DATE,' dd / mm / yyyy')DAYS FROM MANUFACTURED_PRODUCT WHERE ROWNUM = 1 按产品订购_START_DATE;