Anyone can help me with my code?


  • 0
    X

    Write your MySQL query statement below

    SELECT a.Id AS Id FROM Weather AS a, Weather AS b
    WHERE a.Temperature>b.Temperature AND a.Date- b.Date=1;

    The output is exactly the same as the expected output.
    But it says "wrong answer".
    Do I have to use SELECT DATEDIFF instead of simply subtracting the dates?


  • 1
    L

    Well, I guess you need to use the date manipulation function explicitly instead of relying on the interpretation of SQL on the arithmetic operation of Date type.

    I thought also that SQL would know to add a day when I do "Date_next_day = Date + 1". But apparently it did not.

    Note that the date manipulation function API differs across various database. For example, in MySQL, it would be DATE_SUB(date, INTERVAL 1 DAY), while in Sqlite, it is DATE(date, "-1 day").


  • 0
    S

    liaison is right. Also you could do

    SELECT a.Id AS Id FROM Weather AS a, Weather AS b 
    WHERE a.Temperature>b.Temperature AND a.Date= b.Date+INTERVAL 1 DAY;

  • 0
    C

    You can use DateDiff(expr1, expr2) = the value of expr1 sub the value of expr2, as follows
    select w1.Id from Weather w1, Weather w2 where DATEDIFF(w1.Date,w2.Date) = 1 and w1.Temperature > w2.Temperature;


  • 0
    X

    Different functions have different run time.Such as date_add,datediff,to_date.But why they have such difference?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.